Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve local semgrep rules #351

Merged
merged 11 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 79 additions & 7 deletions .semgrep/rules/if-incorrect-nil-err-return.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rules:
- id: if-incorrect-nil-err-return
languages: [go]
languages: [ go ]
severity: WARNING
message: |
WARNING: A local variable '$ERR' is checked for nil, but a different variable is returned.
Expand All @@ -10,12 +10,39 @@ rules:
metavariable: $ERR
regex: .*(?i)err # using .* to allow prefixes, because regex matching is left anchored.

- pattern: |
if $ERR != nil {
...
return ..., $OTHERERR
}
- pattern-either:
# inside multiple return values function or method
- pattern-inside: |
func $FUNCNAME(...) (..., error) {
...
}
- pattern-inside: |
func ($ANYRCV $ANYTYPE) $METHNAME(...) (..., error) {
...
}
# inside single return value function or method
- pattern-inside: |
func $FUNCNAME(...) error {
...
}
- pattern-inside: |
func ($ANYRCV $ANYTYPE) $METHNAME(...) error {
...
}

- pattern-either:
- pattern: | # multiple return values
if $ERR != nil {
...
return ..., $OTHERERR
}
- pattern: | # single return value
if $ERR != nil {
...
return $OTHERERR
}

# pattern-not for multiple return values
- pattern-not: |
if $ERR != nil {
...
Expand All @@ -36,7 +63,7 @@ rules:
...
$NEWERR := $ANYFUNC(..., $ERR, ...)
...
return nil, $NEWERR
return ..., $NEWERR
}
- pattern-not: |
if $ERR != nil {
Expand All @@ -45,3 +72,48 @@ rules:
...
return ..., $ANYFUNC1(..., $NEWERR, ...)
}
- pattern-not: |
if $ERR != nil {
...
$NEWERR := $ERR
...
return ..., $NEWERR
}

# pattern-not for single return value
- pattern-not: |
if $ERR != nil {
...
return $ERR
}
- pattern-not: |
if $ERR != nil {
...
return $ANYFUNC(..., $ERR, ...)
}
- pattern-not: |
if $ERR != nil {
...
return $ANYFUNC(..., $ANYFUNC1(..., $ERR, ...), ...)
}
- pattern-not: |
if $ERR != nil {
...
$NEWERR := $ANYFUNC(..., $ERR, ...)
...
return $NEWERR
}
- pattern-not: |
if $ERR != nil {
...
$NEWERR := $ANYFUNC(..., $ERR, ...)
...
return $ANYFUNC1(..., $NEWERR, ...)
}
- pattern-not: |
if $ERR != nil {
...
$NEWERR := $ERR
...
return $NEWERR
}
72 changes: 65 additions & 7 deletions .semgrep/rules/if-inplace-func-incorrect-nil-err-return.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rules:
- id: if-inplace-func-incorrect-nil-err-return
languages: [go]
languages: [ go ]
severity: WARNING
message: |
WARNING: A local variable '$ERR' is checked for nil, but a different variable is returned.
Expand All @@ -10,12 +10,39 @@ rules:
metavariable: $ERR
regex: .*(?i)err # using .* to allow prefixes, because regex matching is left anchored.

- pattern: |
if $ERR := $FUNC(...); $ERR != nil {
...
return ..., $OTHERERR
}
- pattern-either:
# inside multiple return values function or method
- pattern-inside: |
func $FUNCNAME(...) (..., error) {
...
}
- pattern-inside: |
func ($ANYRCV $ANYTYPE) $METHNAME(...) (..., error) {
...
}
# inside single return value function or method
- pattern-inside: |
func $FUNCNAME(...) error {
...
}
- pattern-inside: |
func ($ANYRCV $ANYTYPE) $METHNAME(...) error {
...
}

- pattern-either:
- pattern: | # multiple return values
if $ERR := $FUNC(...); $ERR != nil {
...
return ..., $OTHERERR
}
- pattern: | # single return value
if $ERR := $FUNC(...); $ERR != nil {
...
return $OTHERERR
}

# pattern-not for multiple return values
- pattern-not: |
if $ERR := $FUNC(...); $ERR != nil {
...
Expand All @@ -36,7 +63,7 @@ rules:
...
$NEWERR := $ANYFUNC(..., $ERR, ...)
...
return nil, $NEWERR
return ..., $NEWERR
}
- pattern-not: |
if $ERR := $FUNC(...); $ERR != nil {
Expand All @@ -45,3 +72,34 @@ rules:
...
return ..., $ANYFUNC1(..., $NEWERR, ...)
}

# pattern-not for single return value
- pattern-not: |
if $ERR := $FUNC(...); $ERR != nil {
...
return $ERR
}
- pattern-not: |
if $ERR := $FUNC(...); $ERR != nil {
...
return $ANYFUNC(..., $ERR, ...)
}
- pattern-not: |
if $ERR := $FUNC(...); $ERR != nil {
...
return $ANYFUNC(..., $ANYFUNC1(..., $ERR, ...), ...)
}
- pattern-not: |
if $ERR := $FUNC(...); $ERR != nil {
...
$NEWERR := $ANYFUNC(..., $ERR, ...)
...
return $NEWERR
}
- pattern-not: |
if $ERR := $FUNC(...); $ERR != nil {
...
$NEWERR := $ANYFUNC(..., $ERR, ...)
...
return $ANYFUNC1(..., $NEWERR, ...)
}