-
Notifications
You must be signed in to change notification settings - Fork 523
Fix condition of fall-through case not used for exhaustive checks #3900
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,30 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug11064; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
interface IClass {} | ||
class ClassA implements IClass {} | ||
class ClassB implements IClass {} | ||
|
||
/** | ||
* @param null|'nil'|IClass $val | ||
*/ | ||
function test($val): string { | ||
switch (true) { | ||
case $val === null: | ||
case $val === 'nil': | ||
assertType("'nil'|null", $val); | ||
return 'null'; | ||
|
||
case $val instanceof ClassA: | ||
assertType('Bug11064\\ClassA', $val); | ||
return 'class a'; | ||
|
||
default: | ||
assertType('Bug11064\\IClass~Bug11064\\ClassA', $val); | ||
throw new RuntimeException('unsupported class: ' . get_class($val)); | ||
} | ||
} | ||
|
This file contains hidden or 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 hidden or 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,32 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.1 | ||
|
||
namespace Bug12722; | ||
|
||
enum states { | ||
case state1; | ||
case statealmost1; | ||
case state3; | ||
} | ||
|
||
class HelloWorld | ||
{ | ||
public function intentional_fallthrough(states $state): int | ||
{ | ||
switch($state) { | ||
|
||
case states::state1: //intentional fall-trough this case... | ||
case states::statealmost1: return 1; | ||
case states::state3: return 3; | ||
} | ||
} | ||
|
||
public function no_fallthrough(states $state): int | ||
{ | ||
switch($state) { | ||
|
||
case states::state1: return 1; | ||
case states::statealmost1: return 1; | ||
case states::state3: return 3; | ||
} | ||
} | ||
} |
This file contains hidden or 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,52 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug3488; | ||
|
||
interface I { | ||
const THING_A = 'a'; | ||
const THING_B = 'b'; | ||
const THING_C = 'c'; | ||
} | ||
|
||
class C | ||
{ | ||
/** | ||
* @param I::THING_* $thing | ||
*/ | ||
public function allCovered(string $thing): int | ||
{ | ||
switch ($thing) { // should not error | ||
case I::THING_A: | ||
case I::THING_B: | ||
case I::THING_C: | ||
return 0; | ||
} | ||
} | ||
/** | ||
* @param I::THING_* $thing | ||
*/ | ||
public function invalidCase(string $thing): int | ||
{ | ||
switch ($thing) { | ||
case I::THING_A: | ||
case I::THING_B: | ||
return 0; | ||
case 'd': | ||
throw new Exception('The error marker should be on the line above'); | ||
} | ||
} | ||
/** | ||
* @param I::THING_* $thing | ||
*/ | ||
public function defaultUnnecessary(string $thing): int | ||
{ | ||
switch ($thing) { | ||
case I::THING_A: | ||
case I::THING_B: | ||
case I::THING_C: | ||
return 0; | ||
default: | ||
throw new Exception('This should be detected as unreachable'); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug8719; | ||
|
||
class HelloWorld | ||
{ | ||
private const CASE_1 = 1; | ||
private const CASE_2 = 2; | ||
private const CASE_3 = 3; | ||
|
||
/** | ||
* @return self::CASE_* | ||
*/ | ||
private function getCase(): int | ||
{ | ||
return random_int(1,3); | ||
} | ||
|
||
public function ok(): string | ||
{ | ||
switch($this->getCase()) { | ||
case self::CASE_1: | ||
$foo = 'bar'; | ||
break; | ||
case self::CASE_2: | ||
$foo = 'baz'; | ||
break; | ||
case self::CASE_3: | ||
$foo = 'barbaz'; | ||
break; | ||
} | ||
|
||
return $foo; | ||
} | ||
|
||
public function not_ok(): string | ||
{ | ||
switch($this->getCase()) { | ||
case self::CASE_1: | ||
$foo = 'bar'; | ||
break; | ||
case self::CASE_2: | ||
case self::CASE_3: | ||
$foo = 'barbaz'; | ||
break; | ||
} | ||
|
||
return $foo; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this error already was incorrect. The other one is now also broken, because its condition is also used