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

S1125: Faulty code fix on C# pattern matching in conditional operator #2618

Closed
rangp opened this issue Sep 2, 2019 · 0 comments · Fixed by #7719
Closed

S1125: Faulty code fix on C# pattern matching in conditional operator #2618

rangp opened this issue Sep 2, 2019 · 0 comments · Fixed by #7719
Assignees
Labels
Area: C# C# rules related issues.
Milestone

Comments

@rangp
Copy link

rangp commented Sep 2, 2019

Description

A bit of an exotic error. The following conditional expression

var isSuccess = y is ISuccessEvaluator evaluator ? evaluator.Evaluate(result) : true;

results in a S1125 warning "remove the unnecessary boolean literal(s)" . The suggested code fix would result in:

var isSuccess = !y is ISuccessEvaluator evaluator || evaluator.Evaluate(result);

which produces a compiler error "! cannot be applied to operand of type "[whatever type y is]".

Expected behavior

var isSuccess = !(y is ISuccessEvaluator evaluator) || evaluator.Evaluate(result);

is the code fix that will not throw a compiler error. The braces are necessary in this instance because otherwise the compiler will try to apply the negation to type y which is not a bool.

Known workarounds

Add the braces manually after applying the code fix

Related information

  • SonarC# Version 7.16.0.8981
  • Visual Studio Version VS 2019 Version 16.2.0 Preview 3.0
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants