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

Enable new analyzers CA1510/11/12/13 and CA1856/57/58 #80149

Merged
merged 4 commits into from
Jan 7, 2023

Conversation

stephentoub
Copy link
Member

CA1510: Use ArgumentNullException throw helper
CA1511: Use ArgumentException throw helper
CA1512: Use ArgumentOutOfRangeException throw helper
CA1513: Use ObjectDisposedException throw helper
CA1856: Incorrect usage of ConstantExpected attribute
CA1857: A constant is expected for the parameter
CA1858: Use 'StartsWith' instead of 'IndexOf'

@ghost
Copy link

ghost commented Jan 3, 2023

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

Issue Details

CA1510: Use ArgumentNullException throw helper
CA1511: Use ArgumentException throw helper
CA1512: Use ArgumentOutOfRangeException throw helper
CA1513: Use ObjectDisposedException throw helper
CA1856: Incorrect usage of ConstantExpected attribute
CA1857: A constant is expected for the parameter
CA1858: Use 'StartsWith' instead of 'IndexOf'

Author: stephentoub
Assignees: stephentoub
Labels:

area-Meta

Milestone: -

eng/Versions.props Outdated Show resolved Hide resolved
@buyaa-n
Copy link
Contributor

buyaa-n commented Jan 4, 2023

JFYI: the analyzer found diagnostics on mono builds, other than that LGTM

Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

CA1510: Use ArgumentNullException throw helper
CA1511: Use ArgumentException throw helper
CA1512: Use ArgumentOutOfRangeException throw helper
CA1513: Use ObjectDisposedException throw helper
CA1856: Incorrect usage of ConstantExpected attribute
CA1857: A constant is expected for the parameter
CA1858: Use 'StartsWith' instead of 'IndexOf'
@stephentoub stephentoub merged commit 699acfa into dotnet:main Jan 7, 2023
@stephentoub stephentoub deleted the enableanalyzers branch January 7, 2023 20:48
<!-- Ignore analyzers that recommend APIs introduced in .NET Core when targeting frameworks that lack those APIs
to avoid issues with multitargeting.
-->
<NoWarn Condition="$(TargetFrameworks.Contains('NetFramework')) or $(TargetFrameworks.Contains('netstandard'))">$(NoWarn);CA1510;CA1511;CA1512;CA1513</NoWarn>
Copy link
Member

@ViktorHofer ViktorHofer Jan 10, 2023

Choose a reason for hiding this comment

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

TargetFrameworks.Contains('NetFramework')) is never true as .NET Framework tfms don't contain such a string in their tfm alias. Instead you want to use TargetFrameworkIdentifier. Using TFI guarantees that the logic works, irrespective of the alias which can be any value.

<NoWarn Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' or '$(TargetFrameworkIdentifier)' == '.NETStandard'">$(NoWarn);CA1510;CA1511;CA1512;CA1513</NoWarn>

In addition to that, we have projects that don't multi-target and only define a TargetFramework property (singular) which would result in the NoWarn not having effect. By using TFI you can avoid these problems.

Copy link
Member Author

Choose a reason for hiding this comment

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

By using TFI you can avoid these problems.

I'm not following. I want these NoWarns to be set when building for .net core if the project also has a net framework or net standard target. How does your proposal achieve that?

Copy link
Member

@ViktorHofer ViktorHofer Jan 10, 2023

Choose a reason for hiding this comment

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

I see, I thought you wanted to set the NoWarn for .NET Framework and .NET Standard builds. Still, $(TargetFrameworks.Contains('NetFramework')) is wrong, we don't have such a TFM. You probably conditioned on the msbuild property name instead of the property value.

We usually use net4 to identify .NET Framework builds in the TargetFrameworks string:

<NoWarn Condition="$(TargetFrameworks.Contains('net4')) or $(TargetFrameworks.Contains('netstandard'))">$(NoWarn);CA1510;CA1511;CA1512;CA1513</NoWarn>

@ghost ghost locked as resolved and limited conversation to collaborators Feb 9, 2023
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