You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First off, thanks for a fantastic tool! :) Been spending a day or two at work to implement it in a large'ish codebase, and it has already proven immensely valuable in finding bad patterns that had slipped through the cracks in review.
I'm trying to write a rule guard that will satisfy the following but can't quite figure out if it is possible or not:
I want to flag any call to check.OK() anywhere within a method named Fix(context.Context)
Calls to check.OK() in other places is fine
Calls to other methods than OK() is fine
typeFixerinterface {
Fix(context.Context) *Entry
}
typeCheckLoggedInImplstruct {
checks.Entry
}
func (check*CheckLoggedInImpl) Fix(ctx context.Context) *checks.Entry {
// .... lots of code hereiftrue {
returncheck.OK() // not working
}
returncheck.OK() // working
}
I've tried a bunch of rules, like the one below, but I can't figure out how to traverse the stack and see what the method name is for the Match(), so I know whether it's a problem or not it's being called.
I got the last return of the Fix() method matching, but I can't figure out how to get any call to check.OK() as reporting - regardless of indentation and what code formatting
// Don't use 'path' package as it doesn't support Windows path separator,// which often leads to bugs and other trouble//// https://stackoverflow.com/a/48050736/1081818funcfixMustCallCheck(m dsl.Matcher) {
m.Import("$INTERNAL_URL/internal/system/checks")
m.
Match(
`func ($_ $implType) Fix($_ context.Context) $_ { $*_ ; return $check.OK() }`,
`func ($_ $implType) Fix($_ context.Context) $_ { $*_ ; { $*_ ; return $check.OK() } ; $*_ }`,
).
Where(m["implType"].Filter(ensureFixer)).
At(m["check"]).
Report(`You should not call check.OK() within a Fix() method - please call check.Check(ctx) instead`)
}
funcensureFixer(ctx*dsl.VarFilterContext) bool {
fixer:=ctx.GetInterface(`$INTERNAL_URL/internal/system/checks.Fixer`)
returntypes.Implements(ctx.Type, fixer)
}
The text was updated successfully, but these errors were encountered:
I made more progress today that seems to detect all calls to .OK() within my method as expected.
The first matcher will correctly highlight the At() for where the check was detected, but the second one captures calls to OK() regardless of conditionals and indentation via the m["$$"].Contains() logic doesn't - it can just highlight the whole matched group.
I couldn't find any method like .At(m["$$"].FindNode("return $name.OK()") which would return the position / node that matched in the .Contains() so it could properly highlight where the match was found. Basically what .Contains() does, but returning the Var() for use in At() rather than a boolean
funcfixMustCallCheck(m dsl.Matcher) {
m.Import("$INTERNAL_URL/internal/system/checks")
m.
Match(
// captures return statement at the end of the func, and allows for proper At() positioning`func ($name $implType) Fix($*_) $_ { $*_ ; return $check.OK() ; $*_ }`,
// Captures the entire body of the func so that the m[$$].Contains can do sub-filtering on calls to $name.OK()// it does not have a way to output the _actual_ position of the issue discovered, but it will complain// and let you know which file+method is affecting, which is all you need to fix.`func ($name $implType) Fix($*_) $_ { $*check }`,
).
Where(m["implType"].Filter(ensureFixer) &&m["$$"].Contains("return $name.OK()")).
At(m["check"]).
Report(`You should not call check.OK() within a Fix() method - please call check.Check(ctx) instead`)
}
Hey!
First off, thanks for a fantastic tool! :) Been spending a day or two at work to implement it in a large'ish codebase, and it has already proven immensely valuable in finding bad patterns that had slipped through the cracks in review.
I'm trying to write a rule guard that will satisfy the following but can't quite figure out if it is possible or not:
check.OK()
anywhere within a method namedFix(context.Context)
check.OK()
in other places is fineOK()
is fineI've tried a bunch of rules, like the one below, but I can't figure out how to traverse the stack and see what the
method
name is for theMatch()
, so I know whether it's a problem or not it's being called.I got the last return of the
Fix()
method matching, but I can't figure out how to get any call tocheck.OK()
as reporting - regardless of indentation and what code formattingThe text was updated successfully, but these errors were encountered: