Skip to content

Comments

Fix #11389 Do not warn for truncLongCastReturn if operands have known valid int#8192

Open
ceJce wants to merge 4 commits intodanmar:mainfrom
ceJce:fix11389_unnecessary_truncLongCastReturn
Open

Fix #11389 Do not warn for truncLongCastReturn if operands have known valid int#8192
ceJce wants to merge 4 commits intodanmar:mainfrom
ceJce:fix11389_unnecessary_truncLongCastReturn

Conversation

@ceJce
Copy link
Contributor

@ceJce ceJce commented Feb 9, 2026

This fix handles the first example in ticket, i.e. when the value is known to be a valid int:

long f() {
int n = 1;
return n << 12;
}

Copy link
Owner

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! I would like some test cases. One case where we don't write a false positive.

And verify that we still warn when we want.

@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch 2 times, most recently from 968f597 to 2c262a0 Compare February 17, 2026 07:55
@ceJce
Copy link
Contributor Author

ceJce commented Feb 17, 2026

thanks! I would like some test cases. One case where we don't write a false positive.

And verify that we still warn when we want.

Tests added.

@ceJce ceJce requested a review from danmar February 18, 2026 08:09
@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch 2 times, most recently from cf1e453 to 88ba04e Compare February 19, 2026 12:49
type->pointer == 0U &&
type->originalTypeName.empty())
type->originalTypeName.empty() &&
!tok->astOperand1()->hasKnownIntValue())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel it's enough to just check if it's a known value. I feel we should check if it is a known value that fits in an int. I would still like to warn here:

long f() {
    unsigned int n = 1U << 20;
    return n << 20;
}

Can you please add a test where we do warn for such code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch from 88ba04e to 452304d Compare February 20, 2026 09:03
@ceJce ceJce force-pushed the fix11389_unnecessary_truncLongCastReturn branch from 452304d to 976db09 Compare February 20, 2026 11:39
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants