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

Fix S1125: Codefix should set correct condition priority by adding parentheses. #7462

Closed
mary-georgiou-sonarsource opened this issue Jun 19, 2023 · 0 comments · Fixed by #7719
Assignees
Labels
Area: C# C# rules related issues. Type: Code Fix Anything related to Code Fixes
Milestone

Comments

@mary-georgiou-sonarsource
Copy link
Contributor

The statement

obj == null ? false : obj.ToString() == "x" || obj.ToString() == "y";

get fixed to:

!(obj == null) && obj.ToString() == "x" || obj.ToString() == "y";

which throws null exception because the condition is evaluated as (condition1 && conction2) || condition3. (condition1 && conction2) is false and so condition3 will be evaluated.

It should be fixed to:

!(obj == null) && (obj.ToString() == "x" || obj.ToString() == "y");

so that the evaluation is condition1 && (condition2 || condition3) and if condition1 is false then (condition2 || condition3) won't be evaluated at all.

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: Code Fix Anything related to Code Fixes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants