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

S2583 FP: When using nullable boolean in if-elseif-else. #4755

Closed
ndsvw opened this issue Aug 3, 2021 · 3 comments · Fixed by #7750
Closed

S2583 FP: When using nullable boolean in if-elseif-else. #4755

ndsvw opened this issue Aug 3, 2021 · 3 comments · Fixed by #7750
Assignees
Labels
Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@ndsvw
Copy link

ndsvw commented Aug 3, 2021

Description

used SonarQube version: 8.9.1

A nullable boolean can have 3 values (true, false, null).

When using "if", "else if" and "else" to write conditions for all 3 cases, S2583 shows a false positive.

Repro steps

Write this code:

		var b = (bool?)objectArg;
		
		if(b == true) {
			Console.WriteLine("true");	
		} else if(b == false) {
			Console.WriteLine("false");	
		} else {
			Console.WriteLine("null");	
		}

Our code:
Bildschirmfoto 2021-08-03 um 13 53 56

Expected behavior

It's not a bug.

"b == false" is not underlined red.

There is no description "Change this condition so that it does not always evaluate to 'true'; some subsequent code is never executed." because it is wrong.

Actual behavior

It underlines "b == false" red with the description "Change this condition so that it does not always evaluate to 'true'; some subsequent code is never executed."

The else part has a red background because SonarDotnet thinks, it will never be executed.

Related information

  • SonarScanner for .NET version (if used): sonar-scanner-4.6.1.2450
  • Operating System: Windows 10
  • SonarQube version: 8.9.1
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels Aug 4, 2021
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S2583 False Positive when using nullable boolean in if-elseif-else. Fix S2583 FP: When using nullable boolean in if-elseif-else. Aug 4, 2021
@pavel-mikula-sonarsource
Copy link
Contributor

Hi @ndsvw,

Thank you for reporting this case. I can confirm it as False Positive. Looks like we don't build proper set of possible constraints for this case.

@pavel-mikula-sonarsource
Copy link
Contributor

The key is that var b = (bool?)objectArg; needs to be in the way. It works well for bool? b argument and bool? b = null; statement.

@andrei-epure-sonarsource andrei-epure-sonarsource changed the title Fix S2583 FP: When using nullable boolean in if-elseif-else. S2583 FP: When using nullable boolean in if-elseif-else. Oct 14, 2022
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 9.7 milestone Jul 24, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource modified the milestones: 9.7, 9.8 Aug 4, 2023
@mary-georgiou-sonarsource
Copy link
Contributor

Fixed by #7750

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. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants