Repository navigation
Open
Description
As a follow up to #5656 each code analysis rule that is marked as Action="Info" should be evaluated and migrated to Action="Warning" once all instances of the violations are fixed or marked with ignore.
To make the process as painless as possible. Each rule should be implemented in its own PR.
Before the merges:
List of rules to enable after resolving occurrences:
- CA1050 - CA1050: Declare types in namespaces #7195 CA1050: Declare types in namespaces #7227
- CA1070 - Set CA1070 to Warning #7231
- CA1200 - CA1200: Avoid using cref tags with a prefix #7194
- CA1507 - Set CA1507 to Warning and fix instances of errors #7176
- CA1802 - CA1802 Fixes and Severity to Warning #7177
- CA1805 - Disable CA1805 #7251
- CA1810 - CA1810 Initialize reference type static fields inline #7179
- CA1823 - CA1823 Avoid unused private fields #7180
- CA1825 - CA1825 Avoid zero-length array allocations #7181
- CA1827 - CA1827 Count() is used where Any() could be used instead to improve p… #7207
- CA1829 - CA1829 Use Length/Count property instead of Count() when available #7182
- CA1834 - CA1834 Consider using 'StringBuilder.Append(char)' when applicable #7183
- CA1835 - CA1835 Prefer the 'Memory'-based overloads for 'ReadAsync' and 'Write… #7184
- CA1836 - CA1836 Prefer IsEmpty over Count #7185
- CA1837 - Cannot use
Environment.ProcessIdbecause it isn't available in older versions of .NET. When the team decides to no longer support versions below .NET 5 then this can be enabled. - CA1838 - CA1838 Avoid 'StringBuilder' parameters for P/Invokes #7186
- CA2007
- CA2008
- CA2016 - CA2016 Forward the 'CancellationToken' parameter to methods that take… #7188
- CA2208 - CA2208 Instantiate argument exceptions correctly #7187
- CA2241 - CA2241 Provide correct arguments to formatting methods #7190
- CA2249 - not totally possible due to required string.contains method signatures not being available in older .NET versions.
- CA3075
- CA3076
- CA3077
- CA5350 - CA5350 Do Not Use Weak Cryptographic Algorithms #7233
- CA5384 - CA5384 Asymmetric encryption algorithm DSA is weak. Switch to an RSA … #7234
- SA0001
- SA1002 - SA1002 The spacing around a semicolon is incorrect #7199
- SA1004 - SA1004 Documentation line should begin with a space #7204
- SA1005 - SA1005 Single line comment should begin with a space #7259
- SA1006 - SA1006 A C# preprocessor-type keyword is preceded by space. #7232
- SA1008
- SA1009
- SA1010 - SA1010 Opening square brackets should not be preceded by a space #7205
- SA1011
- SA1012
- SA1013
- SA1014- SA1014 #7235
- SA1015- SA1015 Closing generic bracket should not be followed by a space #7236
- SA1020- SA1020 Increment symbol '++' should not be preceded by a space #7237
- SA1021
- SA1023 - SA1023 Dereference symbol '*' should not be preceded by a space. #7238
- SA1024 - SA1024 Colon should be followed by a space #7266
- SA1025
- SA1100
- SA1102 - SA1102 Query clause should follow previous clause #7261
- SA1106
- SA1107
- SA1110 - Added IDE analyzers checks and enabled certain checks #8336
- SA1111 - Added IDE analyzers checks and enabled certain checks #8336
- SA1114
- SA1116
- SA1117
- SA1120
- SA1122 - Disable SA1122 (Use string.Empty for empty strings) #7239
- SA1123
- SA1125 - SA1125 use shorthand for Nullable types #7201
- SA1127 - SA1127 Generic type constraints should be on their own line #7267
- SA1128
- SA1130
- SA1131
- SA1132 - SA1132 Do not combine fields #7240
- SA1133 - SA1133 Each attribute should be placed in its own set of square brackets #7262
- SA1134 - SA1134 Each attribute should be placed on its own line of code #7263
- SA1135 - SA1135 A using directive is not qualified #7264
- SA1136 - SA1136 place each enum value on its own line #7203
- SA1137
- SA1201
- SA1202
- SA1203
- SA1204
- SA1208
- SA1209
- SA1210
- SA1211
- SA1214
- SA1216 - SA1216 Using static directives should be placed at the correct location #7265
- SA1217
- SA1300
- SA1302 - SA1302 Interface names should begin with I #7268
- SA1303
- SA1304
- SA1306
- SA1308
- SA1311
- SA1312
- SA1313
- SA1316
- SA1401
- SA1402
- SA1403
- SA1404
- SA1405
- SA1407
- SA1408 - SA1408 Conditional expressions should declare precedence #7269
- SA1413
- SA1414
- SA1500
- SA1501
- SA1502
- SA1503
- SA1504 - SA1504 All accessors should be single-line or multi-line #7270
- SA1505 - SA1505 An opening brace should not be followed by a blank line #7272
- SA1506 - SA1506 Element documentation headers should not be followed by blank … #7271
- SA1507
- SA1508 - SA1508 A closing brace should not be preceded by a blank line #7273
- SA1509 - SA1509 Opening braces should not be preceded by blank line #7274
- SA1510 - Enable SA1510 to Warning #7275
- SA1512
- SA1513
- SA1514
- SA1515
- SA1516
- SA1518 - SA1518 - The line endings at the end of a file do not match the setti… #7571
- SA1519 - SA1519 Braces should not be omitted from multi-line child statement #7276
- SA1520 - SA1520 consistent use of opening and closing braces on if statements #7198
- SA1601
- SA1602
- SA1604
- SA1605
- SA1606
- SA1608
- SA1610
- SA1611
- SA1612
- SA1614
- SA1616
- SA1618
- SA1619
- SA1622
- SA1623
- SA1624
- SA1626- SA1626 single-line comment which begins with three forward slashes in… #7197
- SA1627
- SA1629
- SA1633 - Superseded by IDE0073
- SA1642
- SA1643 - SA1643: Finalizer appropriate summary text #7196
- SA1649
Reacted by Forgind and AraHaan
Reacted by Nirmal Guru
Activity
elachlan commentedon Dec 30, 2021
CA1837: https://docs.microsoft.com/en-au/dotnet/fundamentals/code-analysis/quality-rules/ca1837
We can't use this one because it isn't available in older versions of .NET. When the team decides to no longer support versions below .NET 5 then this can be enabled.
elachlan commentedon Dec 30, 2021
CA2241: https://docs.microsoft.com/en-au/dotnet/fundamentals/code-analysis/quality-rules/CA2241
This has lots of matches in msbuild\src\Build.UnitTests\FileUtilitiesRegex_Tests.cs
Example:
I don't know if this is on purpose. This would just result in an empty string afaik.
elachlan commentedon Dec 30, 2021
ca2249: https://docs.microsoft.com/en-au/dotnet/fundamentals/code-analysis/quality-rules/ca2249
This one also has issues because of the missing overloads on
string.Contains. So not all usages can be converted.elachlan commentedon Dec 30, 2021
@Forgind I can't manage to get StyleCop Analyzers to run anymore. Are they working for you?
Forgind commentedon Dec 30, 2021
The cases I saw looked like they should resolve to an empty string, so I think it's fine to replace those with string.Empty and skip the format part.
Forgind commentedon Dec 30, 2021
Does this also complain about cases like:
? Also, what do you mean by missing overloads? Like string.Contains requires a string, and we sometimes use a char? If that's the case, I'd leave the ones where we use a char; it's more efficient memory-wise to not allocate a string for no reason—though I guess it's probably short enough that the runtime wouldn't actually have to allocate anyway.
Forgind commentedon Dec 30, 2021
Do you mean locally or in PRs? I just tested a local build, and the analyzer seemed to work fine. If PR builds aren't catching it anymore, it might be throttled, but I don't know how to change that.
elachlan commentedon Dec 30, 2021
Locally, I am probably doing it wrong somehow. It has been a long while since I worked on this. Do I use visual studio 2019 release , 2022 release, or 2022 preview?
Forgind commentedon Dec 30, 2021
I don't think it matters. What are you doing to run them? I went to my msbuild repo and ran
build.cmd.msbuild MSBuild.slnalso worked, though it didn't upgrade the warning to an error.elachlan commentedon Dec 30, 2021
yes, there is a char overload and there are functions which take different comparators than
StringComparison.Ordinal. But those are only added in .NET 5.elachlan commentedon Dec 30, 2021
Awesome. I will submit a PR with the fixes.
CA1050: Declare types in namespaces (#7227)
12 remaining items
CA1810 Initialize reference type static fields inline (#7179)
CA1838 Avoid 'StringBuilder' parameters for P/Invokes (#7186)
elachlan commentedon Apr 25, 2022
We need to address this as it is the same issue we ran into for #7571, but its worse because code style can be different between the nuget package code and msbuild.
@sharwell are you able to look at #7174 (comment) and let us know your thoughts?
Nirmal4G commentedon Apr 29, 2022
You should add file scoped namespaces to the list, is there a code for it?
sharwell commentedon Apr 29, 2022
Style rules in .globalconfig will not apply to a correctly authored NuGet package. If you are running into problems with the global NuGet cache, there is a bug in one or more NuGet packages which needs to be corrected (or have a workaround). It's not clear which package(s) or rule(s) are involved in the problem you saw.
rainersigwald commentedon Apr 29, 2022
@sharwell the package in question is
microsoft.codeanalysis.collections, for instance in this check run: https://github.com/dotnet/msbuild/runs/6150398057.sharwell commentedon Apr 29, 2022
There are a few ways to resolve:
generated_codeproperty in .globalconfig for each of the impacted filesDiagnosticSuppressorthat knows how to suppress style rules in the files in this package. This is how Roslyn modified VSTHRD200 behavior after a feature request to add logic to the rule itself was rejected (VSTHRD200 should not trigger on test methods microsoft/vs-threading#670).rainersigwald commentedon Apr 29, 2022
My understanding is that this isn't possible because we don't know an absolute path for the NuGet packages folder. Is that not the case?
sharwell commentedon Apr 29, 2022
I'm not completely sure here. For central builds (with determinism enabled), the paths should be normalized to a
/_/prefix of some sort that's predictable. I'm not sure if there's something similar for local builds.SA1518 - The line endings at the end of a file do not match the setti… (
vlada-shubina commentedon Jan 20, 2023
Code-style rules (IDE) are also useful.
They can be enabled using
dotnet_analyzer_diagnostic.category-Style.severity = warningor individually.vlada-shubina commentedon Jan 20, 2023
SA1010 - #7205
inObjectModelHelpers(shared file), this check is not flagged as warning. Probably worth checking why.ignore me, it is SA1110.
vlada-shubina commentedon Jan 30, 2023
Additional checks to enable once fixed:
stan-sz commentedon Jan 31, 2023
Regarding some of the rules, per @rainersigwald's comment in https://github.com/dotnet/msbuild/pull/7953/files/c517eaada858f113d7bbf6c06cdf6044b5accb15#r1051020281, it does make sense to leave them as warning for the inner-loop agility, while turn warnings to errors at CI builds.