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

Update dependencies from dotnet/roslyn-analyzers, config new analyzers #99343

Merged
merged 6 commits into from
Mar 7, 2024

Conversation

buyaa-n
Copy link
Member

@buyaa-n buyaa-n commented Mar 6, 2024

Related to #99327

@ghost
Copy link

ghost commented Mar 6, 2024

Tagging subscribers to this area: @dotnet/area-infrastructure-libraries
See info in area-owners.md if you want to be subscribed.

Issue Details

Related to #99327

Author: buyaa-n
Assignees: buyaa-n
Labels:

area-Infrastructure-libraries

Milestone: -

dotnet_diagnostic.CA2262.severity = warning

# CA2263: Prefer generic overload when type is known
dotnet_diagnostic.CA2263.severity = info
Copy link
Member

Choose a reason for hiding this comment

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

Is there a problem / difficulty with this diagnostic that prevents us from having it be a warning?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a some cases where the suggested API is calling the type overload internally, I assume we would not like to flag them, let me know if you have other suggestion. There also some that looks could be more efficient and should be fixed, I will update them with this PR, overall around 200 findings with duplicates
CA2263 findings.txt

Copy link
Member

Choose a reason for hiding this comment

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

I think we should go through and prepare the change to fix them, and then evaluate any cases we believe are problematic. Two reasons:

  1. One of the reasons we aggressively consume new analyzers into dotnet/runtime is to vet the new analyzers. dotnet/runtime is a large codebase, and is a good first line of defense against problems with an analyzer's implementation or even high-level goal. It'd be good to understand which of those 200 warnings are real things to be fixed and which are noise that might demand changes to the analyzer.
  2. We're hopefully only adding analyzers we believe lead to making code better. We want that betterness in dotnet/runtime.

Copy link
Member

Choose a reason for hiding this comment

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

(That can/should certainly be separate from this PR.)

Copy link
Member Author

@buyaa-n buyaa-n Mar 6, 2024

Choose a reason for hiding this comment

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

I think we should go through and prepare the change to fix them, and then evaluate any cases we believe are problematic. Two reasons:

Sure, I did evaluate them and concluded that those are not issue with the analyzer and also not worth fixing in the runtime, here is the reasons, please let me know if you have any comment/suggestion:
Excluded these cases from fixing:

  1. Excluded fixing the cases when the project builds for netstandard or .NET framework where the generic overload doesn't exit
  2. Cases where the generic overload implementation just calling the Type overload, like: public T CreateDelegate<T>() where T : Delegate => (T)CreateDelegate(typeof(T));. I think there is nothing to fix in the analyzer, unless we want to check the implementation, which I did not think worth a fix, because the analyzer suggestion is more about AOT compliance, external applications should change to use the generic overload if they care about AOT or similar tooling
  3. The generic overload implementation using typeof(T) internally (this is kind of similar to the case 2 but the implementation not exactly calling the Type overload). For example: Marshal.SizeOf<T>(), Marshal.PtrToStructure<T>(nint) etc. By my evaluation these doesn't look worth fixing, but not that sure with my evaluation. No fix needed for the analyzer with the same reason as above, from AOT perspective this is not false positive.

Here are the all cases that I did not fix: CA2263 findings_NotFixed.txt

(That can/should certainly be separate from this PR.)

I have fixed the ones that worth fixing, PTAL

Copy link
Member Author

@buyaa-n buyaa-n Mar 11, 2024

Choose a reason for hiding this comment

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

This analyzer is first and foremost a code style analyzer/fixer. I do not think it is useless, but I am not sure whether it should be enabled by default. Do we have a prior art for analyzers like that?

Then this rule should start with "IDE" prefix https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/categories#style-rules. I think "IDE" rules should not added in roslyn-analyzers repo, probably in roslyn?

Not sure we could move there ...

Copy link
Member

Choose a reason for hiding this comment

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

In some cases, the fix suggested by the analyzer may result in a minor performance improvement. Example: Replacing Enum.GetName with EnumGetName<TEnum> will avoid boxing. I am not aware of anything else.

Copy link
Member Author

Choose a reason for hiding this comment

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

In some cases, the fix suggested by the analyzer may result in a minor performance improvement. Example: Replacing Enum.GetName with EnumGetName<TEnum> will avoid boxing. I am not aware of anything else.

Yes, and that is why I fixed those cases with this PR, thought the cases where the generic overload implementation just calling the Type overload, will have some perf degradation, for other case like Marshal.SizeOf<T>() and Marshal.SizeOf(Type) the perf impact was not obvious, so did not fix. Anyway, I don't think with the analyzer we could evaluate the perf impact.

Based on your and @stephentoub's feedback I put up a PR that fixes all cases excluding the projects that target down-level platforms and System.Linq.Expression.

Copy link
Member Author

Choose a reason for hiding this comment

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

This analyzer is first and foremost a code style analyzer/fixer.

If that's really the case, we shouldn't have it at all; we're generally not in the business of including style analyzers in this library.

But is that really the case?

I think that is the case, so what we should do with the analyzer now?

Copy link
Member

Choose a reason for hiding this comment

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

I think that is the case, so what we should do with the analyzer now?

The fact that it catches things like changing Enum calls from non-generic to generic makes me still excited about it. I was just fixing up an internal codebase and found it useful specifically for that.

@buyaa-n buyaa-n merged commit c6906ae into dotnet:main Mar 7, 2024
188 of 191 checks passed
@buyaa-n buyaa-n deleted the analyzer-conf branch March 7, 2024 21:48
@github-actions github-actions bot locked and limited conversation to collaborators May 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants