-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
257 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
ExternalProjects/BizHawk.Analyzer/UseSimplerBoolFlipAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
namespace BizHawk.Analyzers; | ||
|
||
using System.Collections.Immutable; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
/// <remarks>shoutouts to SimpleFlips</remarks> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class UseSimplerBoolFlipAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private const string ERR_MSG_SIMPLE = "Use e.g. `b = !b;` instead of `b ^= true;`"; | ||
|
||
private const string ERR_MSG_COMPLEX = $"{ERR_MSG_SIMPLE} (you may want to store part of the expression in a local variable to avoid repeated side-effects or computation)"; | ||
|
||
private static readonly DiagnosticDescriptor DiagUseSimplerBoolFlip = new( | ||
id: "BHI1104", | ||
title: "Don't use ^= (XOR-assign) for inverting the value of booleans", | ||
messageFormat: "{0}", | ||
category: "Usage", | ||
defaultSeverity: DiagnosticSeverity.Error, | ||
isEnabledByDefault: true); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(DiagUseSimplerBoolFlip); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
ISymbol? boolSym = null; | ||
context.RegisterOperationAction( | ||
oac => | ||
{ | ||
static bool IsZeroWorkLocalOrFieldRef(IOperation trunk) | ||
{ | ||
while (trunk.Kind is OperationKind.FieldReference) | ||
{ | ||
if (trunk.ChildOperations.Count is 0) return true; // in the unit test, the node(s) for the implicit `this.` are missing for some reason | ||
trunk = trunk.ChildOperations.First(); | ||
} | ||
return trunk.Kind is OperationKind.InstanceReference or OperationKind.LocalReference; | ||
} | ||
var operation = (ICompoundAssignmentOperation) oac.Operation; | ||
if (operation.OperatorKind is not BinaryOperatorKind.ExclusiveOr) return; | ||
boolSym ??= oac.Compilation.GetTypeByMetadataName("System.Boolean")!; | ||
if (!boolSym.Matches(operation.Type)) return; | ||
if (operation.Value.Kind is not OperationKind.Literal) return; | ||
var lhsOp = operation.Target; | ||
bool lhsIsSimpleExpr; | ||
switch (lhsOp.Kind) | ||
{ | ||
case OperationKind.PropertyReference: | ||
lhsIsSimpleExpr = false; | ||
break; | ||
case OperationKind.FieldReference: | ||
lhsIsSimpleExpr = IsZeroWorkLocalOrFieldRef(lhsOp); | ||
break; | ||
case OperationKind.LocalReference: | ||
lhsIsSimpleExpr = true; | ||
break; | ||
case OperationKind.ArrayElementReference: | ||
lhsIsSimpleExpr = false; | ||
break; | ||
default: | ||
oac.ReportDiagnostic(Diagnostic.Create(DiagUseSimplerBoolFlip, operation.Syntax.GetLocation(), $"Left-hand side of XOR-assign was of an unexpected kind: {lhsOp.GetType().FullName}")); | ||
return; | ||
} | ||
oac.ReportDiagnostic(Diagnostic.Create( | ||
DiagUseSimplerBoolFlip, | ||
operation.Syntax.GetLocation(), | ||
lhsIsSimpleExpr ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning, | ||
additionalLocations: null, | ||
properties: null, | ||
lhsIsSimpleExpr ? ERR_MSG_SIMPLE : ERR_MSG_COMPLEX)); | ||
}, | ||
OperationKind.CompoundAssignment); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
ExternalProjects/BizHawk.AnalyzersTests/BizHawk.Analyzer/UseSimplerBoolFlipAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace BizHawk.Tests.Analyzers; | ||
|
||
using System.Threading.Tasks; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier< | ||
BizHawk.Analyzers.UseSimplerBoolFlipAnalyzer, | ||
Microsoft.CodeAnalysis.Testing.DefaultVerifier>; | ||
|
||
[TestClass] | ||
public sealed class UseSimplerBoolFlipAnalyzerTests | ||
{ | ||
[TestMethod] | ||
public Task CheckMisuseOfXORAssignment() | ||
=> Verify.VerifyAnalyzerAsync(""" | ||
public static class Cases { | ||
private static int _z = default; | ||
private static bool _a = default; | ||
private static void CasesMethod() { | ||
_z ^= 0xA5A5; | ||
_a ^= DummyMethod(); | ||
{|BHI1104:_a ^= true|}; | ||
var b = false; | ||
{|BHI1104:b ^= true|}; | ||
bool c = default; | ||
{|BHI1104:c ^= false|}; // this is effectively a no-op so there's no reason it would be used in the first place, but it was easier to flag both | ||
{|BHI1104:AnotherClass.GetInstance().Prop ^= true|}; // care needs to be taken with non-trivial expressions like this; a different message will be given | ||
} | ||
private static bool DummyMethod() | ||
=> default; | ||
} | ||
public sealed class AnotherClass { | ||
public static AnotherClass GetInstance() | ||
=> new(); | ||
public bool Prop { get; set; } | ||
private AnotherClass() {} | ||
} | ||
"""); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.