Add linter to error on unchecked map reads #4752
Replies: 5 comments
-
| I checked staticcheck's docs and found related lint rules: 
 But no rule to enforce the guard when it's necessary (inverse of S1036) I created dominikh/go-tools#1328 to track adding this feature to staticcheck | 
Beta Was this translation helpful? Give feedback.
-
| Hello, From a certain perspective, your proposal is close to forcetypeassert | 
Beta Was this translation helpful? Give feedback.
-
| Update: dominikh/go-tools#1328 has been closed as out of scope for staticcheck: From dominikh/go-tools#1328 (comment) 
 | 
Beta Was this translation helpful? Give feedback.
-
| 
 @ldez Agreed, forcetypeassert looks very similar. Ideally the linter should look for  It might be complicated for expressions which are not directly part of an assignment - an additional restriction might be necessary to ensure IndexExpr on map values are always an assignment | 
Beta Was this translation helpful? Give feedback.
-
| Hey @mattysweeps! We've ran into the same requirement at work, did you ever find a tool to help lint this? | 
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Your feature request related to a problem? Please describe.
The following code sample highlights an issue where a map reference was unchecked (did not check ok == true) resulting in a nil pointer panic.
https://go.dev/play/p/XwjkvaEshlu
Describe the solution you'd like.
A linter to catch map references which only return 1 result, instead of both the value and the check.
Describe alternatives you've considered.
None
Additional context.
I looked at golangci-lint's linters but couldn't tell if this is already solved by existing linters.
I tested with golangci-lint
1.50.0with flags--max-issues-per-linter 0 --enable-all --verbosebut did not see any lint results related to the unsafe map usage.Beta Was this translation helpful? Give feedback.
All reactions