Skip to content

Conversation

@Youssef1313
Copy link
Member

@Youssef1313 Youssef1313 commented Jul 8, 2022

Replaces #62150

Previously, we report diagnostics with IsSuppressed if the code-style option is disabled. The use of IsSuppressed in this case is suspicious, this property is related to source (attribute/pragma) suppressions. If code-style option is disabled, no diagnostic should be reported.

@mavasani for review

Core changes are in DiagnosticHelper.cs

@Youssef1313 Youssef1313 requested a review from a team as a code owner July 8, 2022 07:04
@ghost ghost added Community The pull request was submitted by a contributor who is not a Microsoft employee. Area-IDE labels Jul 8, 2022
/// <param name="messageArgs">Arguments to the message of the diagnostic.</param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
public static DiagnosticWrapper Create(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a new wrapper type? Can't we nullable enable all the calling code and have this method return Diagnostic??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mavasani All analyzers will have to do the null check. I wanted to minimize changes to analyzers.

And an extension method named ReportDiagnostic will not picked up unless the parameter type is different (the compiler will prefer the existing ReportDiagnostic over the extension)
And Adding an extension with a new name will lead to updates in all analyzers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, so we are basically trading off the cleanest API to avoid making tons of changes in the code base. That seems reasonable to me, but I'll let others in @dotnet/roslyn-analysis share their views.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meant to tag @dotnet/roslyn-ide

/// </param>
/// <param name="message">Localizable message for the diagnostic.</param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic CreateWithMessage(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still unsure if removing this overload and LocalizableStringWithArguments is required for this PR. Can you clarify why it is being done here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LocalizableStringWithArguments was originally introduced to support option = value:none. Since this PR introduces a new way to support none, the type is no longer needed.

This has also caught extra period in analyzer messages.

var diagnostic = DiagnosticHelper.CreateWithMessage(s_unusedParameterRule, location,
option.Notification.Severity, additionalLocations: null, properties: null, message);
reportDiagnostic(diagnostic);
var diagnostic = DiagnosticHelper.Create(rule, location,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be much cleaner to rename this method to TryCreate and have it return a Diagnostic?. Also you can easily avoid the null check at each callsite by having a helper TryCreateAndReport(..., reportDiagnostic).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mavasani My intent was to avoid too many changes to analyzers.

@Youssef1313
Copy link
Member Author

Youssef1313 commented Jul 21, 2022

Converting to a draft, I'm thinking of a larger refactoring which (if accepted) might make this change easier.

Note to self (just in case I forgot what I have in mind 😄):

  • Introduce an AbstractBuiltInCodeStyleDiagnosticAnalyzerWithOption that inherits from the existing AbstractBuiltInCodeStyleDiagnosticAnalyzer
  • Move option-related constructors to the new class, and let analyzers inherit from the new class.
  • The new class will have an abstract method IsOptionEnabled.
  • Introduce new AnalysisContext structs, that wraps those of the compiler. The new IDE contexts will call IsOptionEnabled and bail-out early if it returns false.

The new IsOptionEnabled will prevent bugs like #62310

If we have now our own analysis contexts that delegate to the compiler, we could simply allow a null diagnostic.

The design above has a flaw for analyzers with multiple options, but hopefully it won't be too hard to figure out a good way for these analyzers.

@Youssef1313 Youssef1313 marked this pull request as draft July 21, 2022 05:36
@Youssef1313
Copy link
Member Author

Youssef1313 commented Jul 26, 2022

Marking as ready for review again. The refactoring I described in previous comment isn't going well.. :(

@Youssef1313 Youssef1313 marked this pull request as ready for review July 26, 2022 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants