Stylecopanalyzers: Is there an analyzer to reporting missing parentheses in IF statements?

Created on 14 Dec 2020  路  5Comments  路  Source: DotNetAnalyzers/StyleCopAnalyzers

Hi StyleCop enthusiasts!

I wonder if there is any analyzer that would catch for me that this line is missing parentheses:

else if (httpResponse.StatusCode >= HttpStatusCode.Ambiguous && httpResponse.StatusCode < HttpStatusCode.BadRequest)

as I would like to enforce this style:

else if ((httpResponse.StatusCode >= HttpStatusCode.Ambiguous) && (httpResponse.StatusCode < HttpStatusCode.BadRequest))

Thank you!

question

All 5 comments

This is built in VS parentheses preferences

You can add the following to your .editorconfig file:

dotnet_style_parentheses_in_other_binary_operators = always_for_clarity

Thank you! I have tried this but I can't see any new warning. Perhaps my MSVS is broken somehow and it will get fixed in a next release.

This parentheses preference is not enforced because there is only one interpretation that makes sense. In other words, readers are unlikely to read the expression as follows, even if they are new to the C# language:

httpResponse.StatusCode >= (HttpStatusCode.Ambiguous && httpResponse.StatusCode) < HttpStatusCode.BadRequest

I'm going to resolve this one as answered from StyleCop Analyzers (analysis is not supported, and would not be implemented because the readability benefits of enforcing this style do not outweigh the maintenance complexity of inserting all these additional parentheses). It could be implemented as an independent analyzer, or perhaps a request filed in dotnet/roslyn.

Thank you for the explanation!

Was this page helpful?
0 / 5 - 0 ratings