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

Bump AnalysisLevel and enable more analyzers #3681

Merged
merged 7 commits into from
Aug 14, 2023
Merged

Bump AnalysisLevel and enable more analyzers #3681

merged 7 commits into from
Aug 14, 2023

Conversation

Morilli
Copy link
Collaborator

@Morilli Morilli commented Jun 9, 2023

This bumps the .NET analyzer level from 5 to 6 and enables more built-in rules from the most useful categories imho.

Note that all analyzers will still only be ran with -p:RunAnalyzersDuringBuild=true currently as build time does increase substantially with those enabled.

When fixing inspections I've tried my best to keep the behavior 100% the same, although some things could break, for example the locale fixes (like float.ToString()).

Some more links: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-options, https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-quality-rule-options

Check if completed:

fixes CA1304, CA1309, CA1310, CA1311

temporarily silences CA1305

reduce severity of CA2101
fixes CA1841

silence CA1805 and CA1822 because of sheer amount of violations

reduce severity of CA1838 because I don't wanna fix it lol
fixes CA2211, CA2215, CA2241, CA2251

silence CA1816 because it's unnecessary

reduce severity of CA2201 because I like the concept but don't wanna fix them all
@Morilli Morilli merged commit c10d292 into master Aug 14, 2023
@Morilli Morilli deleted the more-analyzer branch August 14, 2023 21:18
Copy link
Member

@YoshiRulz YoshiRulz left a comment

Choose a reason for hiding this comment

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

Was in the "middle" (just started really) of a review, will just come back later and commit fixes directly

@@ -461,233 +461,6 @@
<!-- Use the Regex source generator -->
<Rule Id="MA0110" Action="Error" />
</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.FxCopAnalyzers" RuleNamespace="Microsoft.CodeAnalysis.FxCopAnalyzers">
Copy link
Member

Choose a reason for hiding this comment

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

When you say "legacy," have these been removed from the FxCop NuGet package?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The FxCop nuget package itself was no longer included in BizHawk and has been deprecated in favor of the .NET analyzers themselves, see for example https://learn.microsoft.com/en-us/visualstudio/code-quality/migrate-from-legacy-analysis-to-net-analyzers?view=vs-2022.

@@ -165,5 +166,9 @@ public static byte[] ToCharCodepointArray(this string str)
/// <remarks><c>"abc,def,ghi".TransformFields(',', s => s.Reverse()) == "cba,fed,ihg"</c></remarks>
public static string TransformFields(this string str, char delimiter, Func<string, string> transform)
=> string.Join(delimiter.ToString(), str.Split(delimiter).Select(transform));

public static bool StartsWithOrdinal(this string str, string value) => str.StartsWith(value, StringComparison.Ordinal);
Copy link
Member

Choose a reason for hiding this comment

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

This is a good idea to include if the stdlib is missing it, but the downside of rolling your own is that it's going to be overlooked unless there's an Analyzer rule for it. I can add one to BizHawk.Analyzer if you'd like. Also they should take ReadOnlySpan<char>, I've been meaning to do that for the others.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This specific function could be overlooked (I'd appreciate an analyzer rule for that in case you agree with this being here), but the general warning will still display due to CA1310.

var argDowncased = arg.ToLower();
if (argDowncased.StartsWith("--load-slot="))
var argDowncased = arg.ToLowerInvariant();
if (argDowncased.StartsWithOrdinal("--load-slot="))
Copy link
Member

Choose a reason for hiding this comment

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

Here we see the limits of low-effort (no offence) find+replace: these could all be StartsWith(..., StringComparison.OrdinalIgnoreCase) (or a helper for brevity). Maybe this way happens to be more performant, but I'd argue readability is more important for this method.

I don't consider this blocking for merge as you're just resolving warnings.

@@ -71,7 +71,7 @@ public bool Remove(string newFile)
{
if (!Frozen)
{
return recentlist.RemoveAll(recent => string.Compare(newFile, recent, StringComparison.CurrentCultureIgnoreCase) == 0) != 0; // none removed => return false
return recentlist.RemoveAll(recent => string.Equals(newFile, recent, StringComparison.OrdinalIgnoreCase)) != 0; // none removed => return false
Copy link
Member

Choose a reason for hiding this comment

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

string.Compare(a, b, m) == 0 => string.Equals(a, b, m)?

(Should write an Analyzer rule for this too...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -65,7 +65,7 @@ public RomGame(HawkFile file, string patch)

if (!string.IsNullOrWhiteSpace(GameInfo.Name) && GameInfo.Name == GameInfo.Name.ToUpperInvariant())
{
GameInfo.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(GameInfo.Name.ToLower());
GameInfo.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(GameInfo.Name.ToLowerInvariant());
Copy link
Member

Choose a reason for hiding this comment

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

What is even going on here


public static bool StartsWithOrdinal(this string str, string value) => str.StartsWith(value, StringComparison.Ordinal);

public static bool EndsWithOrdinal(this string str, string value) => str.EndsWith(value, StringComparison.Ordinal);
Copy link
Member

Choose a reason for hiding this comment

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

Could do with {,Last}IndexOfOrdinal too

@@ -109,13 +111,13 @@ private static IDecodeResult Gen(string code)
private static IDecodeResult Sms(string code)
{
// Game Genie
if (code.LastIndexOf("-") == 7 && code.IndexOf("-") == 3)
if (code.LastIndexOf("-", StringComparison.Ordinal) == 7 && code.IndexOf("-", StringComparison.Ordinal) == 3)
Copy link
Member

Choose a reason for hiding this comment

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

These checks are dumb, this main class should only do a length check, if applicable, then delegate to the individual decoders and return if successful, trying the next decoder if not. But all of this is dumb and needs rewriting with unit tests.

For this PR, you could replace s.IndexOf(c, Ordinal) == n with s[n] is c and it would be no more broken while resolving the warnings.

@@ -90,12 +90,12 @@ private static string AbsolutePathForInner(this PathEntryCollection collection,

if (path == "%recent%") return PathUtils.SpecialRecentsDir;
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 the == operator is .Equals(..., Ordinal), but it would be nice to be explicit—is there not a rule for that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is https://www.jetbrains.com/help/resharper/SpecifyStringComparison.html, I'm not aware of any global code inspection though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants