1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Allow int casts if the type comes from calculation (#4768)

This commit is contained in:
orklah 2020-12-04 05:15:07 +01:00 committed by Daniil Gentili
parent c60eada3f7
commit 48f55f3c3b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 21 additions and 13 deletions

View File

@ -48,20 +48,22 @@ class CastAnalyzer
if ($maybe_type) {
if ($maybe_type->isInt()) {
if ($maybe_type->from_docblock) {
$issue = new RedundantCastGivenDocblockType(
'Redundant cast to ' . $maybe_type->getKey() . ' given docblock-provided type',
new CodeLocation($statements_analyzer->getSource(), $stmt)
);
} else {
$issue = new RedundantCast(
'Redundant cast to ' . $maybe_type->getKey(),
new CodeLocation($statements_analyzer->getSource(), $stmt)
);
}
if (!$maybe_type->from_calculation) {
if ($maybe_type->from_docblock) {
$issue = new RedundantCastGivenDocblockType(
'Redundant cast to ' . $maybe_type->getKey() . ' given docblock-provided type',
new CodeLocation($statements_analyzer->getSource(), $stmt)
);
} else {
$issue = new RedundantCast(
'Redundant cast to ' . $maybe_type->getKey(),
new CodeLocation($statements_analyzer->getSource(), $stmt)
);
}
if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) {
// fall through
if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) {
// fall through
}
}
}

View File

@ -791,6 +791,12 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
}
}'
],
'noRedundantCastAfterCalculation' => [
'<?php
function x(string $x): int {
return (int) (hexdec($x) + 1);
}',
],
];
}