Created
August 22, 2020 21:27
-
-
Save dnwe/872f66df252a7fb8a4405e7b6448b199 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/ruleguard.rules.go b/ruleguard.rules.go | |
index 5d4c4fe..8d2f8c1 100644 | |
--- a/ruleguard.rules.go | |
+++ b/ruleguard.rules.go | |
@@ -130,6 +130,34 @@ func errnoterror(m fluent.Matcher) { | |
Report("err variable not error type") | |
} | |
+// err found but return nil | |
+func errfoundreturnnil(m fluent.Matcher) { | |
+ m.Match( | |
+ "if $*_, err := $x; $err != nil { return nil } else if $_ { $*_ }", | |
+ "if $*_, err := $x; $err != nil { return nil } else { $*_ }", | |
+ "if $*_, err := $x; $err != nil { return nil }", | |
+ | |
+ "if $*_, err = $x; $err != nil { return nil } else if $_ { $*_ }", | |
+ "if $*_, err = $x; $err != nil { return $*_, nil } else { $*_ }", | |
+ "if $*_, err = $x; $err != nil { return $*_, nil }", | |
+ | |
+ "if err := $x; $err != nil { return nil } else if $_ { $*_ }", | |
+ "if err := $x; $err != nil { return $*_, nil } else { $*_ }", | |
+ "if err := $x; $err != nil { return $*_, nil }", | |
+ | |
+ "if err := $x; $err != nil { return $*_, nil } else { $*_ }", | |
+ "if err := $x; $err != nil { return $*_, nil }", | |
+ | |
+ "if err = $x; $err != nil { return $*_, nil } else { $*_ }", | |
+ "if err = $x; $err != nil { return $*_, nil }", | |
+ | |
+ "if $err != nil { return nil }", | |
+ "if $err != nil { return $*_, nil }", | |
+ ). | |
+ Where(m["err"].Type.Is("error")). | |
+ Report("err value found but return value was nil") | |
+} | |
+ | |
// Identical if and else bodies | |
func ifbodythenbody(m fluent.Matcher) { | |
m.Match("if $*_ { $body } else { $body }"). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment