roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

RCS1051: consider option `do_not_parenthesize_singleline_conditional_expression`

Open anreton opened this issue 3 years ago • 1 comments

RCS1051

Product and Version Used: Microsoft Visual Studio Community 2019 Version 16.10.1 Roslynator.Analyzers 3.2.0 via Nuget

Steps to Reproduce: .editorconfig:

dotnet_diagnostic.RCS1051.severity = warning
roslynator.RCS1051.do_not_parenthesize_single_token = true
public class Foo
{
    public bool Bar { get; }
}

...
var foo = new Foo();
foo.Bar ? 42 : 666; // <-

Actual Behavior: Get diagnostic for RCS1051.

Expected Behavior: No diagnostic for RCS1051.

If we add parentheses, then IDE0047 works. Perhaps the new option do_not_parenthesize_singleline_conditional_expression = bool for RCS1051 would be helpful for this case.

anreton avatar Jun 12 '21 19:06 anreton

I was thinking of something similar but rather than have an option for not parenthesizing a single line, have an option for not parenthesizing a single term instead.

Slightly modified version of code here:

public class Foo
{
    public bool Bar { get; }
    public string Thing {get; }
}

...
var foo = new Foo();
foo.Bar ? 42 : 666; // OK because foo.Bar is a single term
!foo.Bar ? 42 : 666; // OK because !foo.Bar is a single term
foo.Bar && !string.IsNullOrEmpty foo.Thing ? 42 : 666;  // Not OK because multiple terms

To identify if multiple terms, check if there is a binary or tertiary operator in operand? 🤔

amehta-rms avatar Jun 29 '21 16:06 amehta-rms