Skip to content

Commit

Permalink
linter: suppress in dynamic rules (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio authored Dec 18, 2024
1 parent 069d518 commit 9f0fd73
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/linter/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (d *rootWalker) EnterNode(n ir.Node) (res bool) {
if !strings.HasSuffix(n.InterfaceName.Value, "able") {
d.checker.CheckIdentMisspellings(n.InterfaceName)
}

case *ir.ClassStmt:
d.currentClassNodeStack.Push(n)

Expand Down Expand Up @@ -225,6 +226,8 @@ func (d *rootWalker) EnterNode(n ir.Node) (res bool) {

d.scope().AddVar(v, solver.ExprTypeLocal(d.scope(), d.ctx.st, n.Expr), "global variable", meta.VarAlwaysDefined)
case *ir.FunctionStmt:
d.currentClassNodeStack.Push(n)

if d.metaInfo().IsIndexingComplete() {
res = d.checker.CheckFunction(n)
} else {
Expand Down Expand Up @@ -1746,12 +1749,32 @@ func (d *rootWalker) renderRuleMessage(msg string, n ir.Node, m phpgrep.MatchDat
return msg
}

func (d *rootWalker) isSuppressed(n ir.Node, checkName string) bool {
if containLinterSuppress(n, checkName) {
return true
}

// We go up the tree in search of a comment that disables this checker.
for i := 0; d.currentClassNodeStack.NthParent(i) != nil; i++ {
parent := d.currentClassNodeStack.NthParent(i)
if containLinterSuppress(parent, checkName) {
return true
}
}

return false
}

func (d *rootWalker) runRule(n ir.Node, sc *meta.Scope, rule *rules.Rule) bool {
m, ok := rule.Matcher.Match(n)
if !ok {
return false
}

if d.isSuppressed(n, rule.Name) {
return false
}

matched := false
if len(rule.Filters) == 0 {
matched = true
Expand Down
74 changes: 74 additions & 0 deletions src/tests/checkers/linter_suppress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,77 @@ function g($b) {
`)
test.RunAndMatch()
}

func TestLinterSuppressDNRInsideFunction(t *testing.T) {
rfile := `<?php
/**
* @name emptyIf
* @warning suspicious empty body of the if statement
* @scope local
*/
if ($_);
`

test := linttest.NewSuite(t)
test.RuleFile = rfile
test.AddFile(`<?php
if (123); // No warning
function f() {
/**
* @noverify-suppress emptyIf
*/
if (123); // Warning
}
`)
test.RunRulesTest()
}

func TestLinterSuppressDNRBehindFunction(t *testing.T) {
rfile := `<?php
/**
* @name emptyIf
* @warning suspicious empty body of the if statement
* @scope local
*/
if ($_);
`

test := linttest.NewSuite(t)
test.RuleFile = rfile
test.AddFile(`<?php
if (123); // No warning
/**
* @noverify-suppress emptyIf
*/
function f() {
if (123); // Warning
}
`)
test.RunRulesTest()
}

func TestLinterSuppressDNRMethod(t *testing.T) {
rfile := `<?php
/**
* @name emptyIf
* @warning suspicious empty body of the if statement
* @scope local
*/
if ($_);
`

test := linttest.NewSuite(t)
test.RuleFile = rfile
test.AddFile(`<?php
class Foo {
/**
* @noverify-suppress emptyIf
*/
public function f() {
if (123); // Warning
}
}
`)
test.RunAndMatch()
}

0 comments on commit 9f0fd73

Please sign in to comment.