-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make NoEmptyIfDefinedPlugin check all node types except AST_DIM
This should be perfectly fine because the expression should never be unset. Bug: T349446 Change-Id: Icb9f1d1ff6eb678a70609b842b7898667aeb09e1
- Loading branch information
Showing
6 changed files
with
103 additions
and
27 deletions.
There are no files selected for viewing
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
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
3 changes: 3 additions & 0 deletions
3
tests/plugins/NoEmptyIfDefinedPlugin/functions/expectedResults.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
plugins/NoEmptyIfDefinedPlugin/functions/test.php:18 MediaWikiNoEmptyIfDefined Found usage of empty() on expression mayReturnNull() that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/functions/test.php:23 MediaWikiNoEmptyIfDefined Found usage of empty() on expression $class->nonStaticMethod() that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/functions/test.php:26 MediaWikiNoEmptyIfDefined Found usage of empty() on expression TestNoEmptyOnMethodCalls::staticMethod() that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
function mayReturnNull(): ?int { | ||
return rand() ? 42 : null; | ||
} | ||
|
||
class TestNoEmptyOnMethodCalls { | ||
public function nonStaticMethod(): ?int { | ||
return rand() ? 42 : null; | ||
} | ||
|
||
public static function staticMethod(): ?int { | ||
return rand() ? 42 : null; | ||
} | ||
} | ||
|
||
function testFunctionCalls() { | ||
if ( empty( mayReturnNull() ) ) { | ||
echo "hello"; | ||
} | ||
|
||
$class = new TestNoEmptyOnMethodCalls(); | ||
if ( empty( $class->nonStaticMethod() ) ) { | ||
echo "hello"; | ||
} | ||
if ( empty( TestNoEmptyOnMethodCalls::staticMethod() ) ) { | ||
echo "hello"; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/plugins/NoEmptyIfDefinedPlugin/miscnodes/expectedResults.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:6 MediaWikiNoEmptyIfDefined Found usage of empty() on expression 1 that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:7 MediaWikiNoEmptyIfDefined Found usage of empty() on expression (2 + 2) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:8 MediaWikiNoEmptyIfDefined Found usage of empty() on expression true that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:9 MediaWikiNoEmptyIfDefined Found usage of empty() on expression 1 that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:10 MediaWikiNoEmptyIfDefined Found usage of empty() on expression rand() that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:11 MediaWikiNoEmptyIfDefined Found usage of empty() on expression ($x = null) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:13 MediaWikiNoEmptyIfDefined Found usage of empty() on expression ($arr[42] = 42) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:14 MediaWikiNoEmptyIfDefined Found usage of empty() on expression ($y = 42) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:15 MediaWikiNoEmptyIfDefined Found usage of empty() on expression empty($z) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:16 MediaWikiNoEmptyIfDefined Found usage of empty() on expression (fn) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:17 MediaWikiNoEmptyIfDefined Found usage of empty() on expression (true && false) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:18 PhanUndeclaredVariable Variable $x1 is undeclared (Did you mean $x) | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:19 MediaWikiNoEmptyIfDefined Found usage of empty() on expression isset($x2) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:20 PhanUndeclaredVariable Variable $x3 is undeclared (Did you mean $x) | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:20 PhanUndeclaredVariable Variable $x4 is undeclared (Did you mean $x) | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:21 MediaWikiNoEmptyIfDefined Found usage of empty() on expression new stdClass() that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:22 PhanTypeInvalidUnaryOperandIncOrDec Invalid operator: unary operand of (expr)++ is null= (expected int or string or float) | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:22 PhanUndeclaredVariable Variable $doesNotExist is undeclared | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:24 MediaWikiNoEmptyIfDefined Found usage of empty() on expression (clone($obj)) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE | ||
plugins/NoEmptyIfDefinedPlugin/miscnodes/test.php:25 MediaWikiNoEmptyIfDefined Found usage of empty() on expression require($GLOBALS['some_file']) that appears to be always set. empty() should only be used to suppress errors. See https://w.wiki/6paE |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
// @phan-file-suppress PhanNoopEmpty | ||
|
||
function doTestMisc() { | ||
empty( 1 ); | ||
empty( 2 + 2 ); | ||
empty( true ); | ||
empty( ( 1 ) ); | ||
empty( ( rand() ) ); | ||
empty( $x = null ); | ||
$arr = []; | ||
empty( $arr[42] = 42 ); | ||
empty( $y = 42 ); | ||
empty( empty( $z ) ); | ||
empty( fn () => 42 ); | ||
empty( true && false ); | ||
empty( (int)$x1 ); // Redundant empty(), but not reported because an issue is emitted for the expr | ||
empty( isset( $x2 ) ); | ||
empty( rand() ? $x3 : $x4 ); // Redundant empty(), but not reported because an issue is emitted for the expr | ||
empty( new stdClass ); | ||
empty( $doesNotExist++ ); // Redundant empty(), but not reported because an issue is emitted for the expr | ||
$obj = new stdClass; | ||
empty( clone $obj ); | ||
empty( require $GLOBALS['some_file'] ); | ||
} |