Skip to content
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

SE: Concatenate string expression returns non-null string #7111

Closed
martin-strecker-sonarsource opened this issue Apr 25, 2023 · 0 comments · Fixed by #7285
Closed

SE: Concatenate string expression returns non-null string #7111

martin-strecker-sonarsource opened this issue Apr 25, 2023 · 0 comments · Fixed by #7285
Assignees
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues.
Milestone

Comments

@martin-strecker-sonarsource
Copy link
Contributor

martin-strecker-sonarsource commented Apr 25, 2023

According to §12.10.5 a string concatenation never produces a null string:

These overloads of the binary + operator perform string concatenation. If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string operand is converted to its string representation by invoking the virtual ToString method inherited from type object. If ToString returns null, an empty string is substituted.

string s = null;
s = s + null;
s.ToString(); // S2259: FP - s is not null here

Other SE rules are affected as well. Tests are added as part of #7039 in commit 09cf5fb

@martin-strecker-sonarsource martin-strecker-sonarsource added Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels Apr 25, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource changed the title Fix S2259 FP/FN: Concatenate string expression returns non-null string Fix S2259 FP: Concatenate string expression returns non-null string Apr 25, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 9.1 milestone May 19, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource removed their assignment May 24, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource modified the milestones: 9.1, 9.2 May 25, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 9.2 milestone May 26, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Fix S2259 FP: Concatenate string expression returns non-null string SE: Concatenate string expression returns non-null string May 26, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: Improvement and removed Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants