This commit is contained in:
Sebastien Lebreton 2024-02-08 08:04:34 +01:00 коммит произвёл GitHub
Родитель d6e57e3181
Коммит 9ac6cc684b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
28 изменённых файлов: 44 добавлений и 60 удалений

Просмотреть файл

@ -10,7 +10,6 @@ namespace Microsoft.Unity.Analyzers.Tests;
public class AssetOperationInLoadAttributeMethodTests : BaseDiagnosticVerifierTest<AssetOperationInLoadAttributeMethodAnalyzer>
{
[Fact]
public async Task TestValidMethodUsage()
{

Просмотреть файл

@ -158,5 +158,4 @@ class Camera : MonoBehaviour
await VerifyCSharpFixAsync(test, fixedTest);
}
}

Просмотреть файл

@ -115,6 +115,4 @@ class Camera : MonoBehaviour
await VerifyCSharpDiagnosticAsync(test);
}
}

Просмотреть файл

@ -76,7 +76,7 @@ class Camera : MonoBehaviour
await VerifyCSharpDiagnosticAsync(test);
}
[Fact]
public async Task ImproperlyCasedRealStaticMessage()
{
@ -211,5 +211,4 @@ class App : AssetPostprocessor
";
await VerifyCSharpFixAsync(test, fixedTest);
}
}

Просмотреть файл

@ -402,5 +402,4 @@ class Loader
await VerifyCSharpFixAsync(test, fixedTest);
}
}

Просмотреть файл

@ -266,5 +266,4 @@ class App : AssetPostprocessor
await VerifyCSharpDiagnosticAsync(test);
}
}

Просмотреть файл

@ -278,5 +278,4 @@ class Camera : MonoBehaviour
";
await VerifyCSharpFixAsync(test, fixedTest);
}
}

Просмотреть файл

@ -37,7 +37,7 @@ namespace Assets.Scripts
}
}
";
var context = AnalyzerVerificationContext.Default
.WithLanguageVersion(LanguageVersion.CSharp8)
.WithAnalyzerFilter("CS0169");
@ -47,12 +47,12 @@ namespace Assets.Scripts
DiagnosticResult.CompilerWarning(NullableReferenceTypesSuppressor.Rule.SuppressedDiagnosticId)
.WithMessageFormat(WarningFormat)
.WithArguments("field", "field1")
.WithLocation(9, 28),
.WithLocation(9, 28),
DiagnosticResult.CompilerWarning(NullableReferenceTypesSuppressor.Rule.SuppressedDiagnosticId)
.WithMessageFormat(WarningFormat)
.WithArguments("property", "property1")
.WithLocation(11, 28),
.WithLocation(11, 28),
];
await VerifyCSharpDiagnosticAsync(context, test, diagnostics);
@ -87,7 +87,7 @@ namespace Assets.Scripts
}
}
}
";
";
var context = AnalyzerVerificationContext.Default
.WithLanguageVersion(LanguageVersion.CSharp8)

Просмотреть файл

@ -69,5 +69,4 @@ class Camera : MonoBehaviour
await VerifyCSharpDiagnosticAsync(test);
}
}

Просмотреть файл

@ -94,5 +94,4 @@ class Context
await VerifyCSharpFixAsync(test, fixedTest);
}
}

Просмотреть файл

@ -324,5 +324,4 @@ class Camera : MonoBehaviour
await VerifyCSharpFixAsync(test, fixedTest);
}
}

Просмотреть файл

@ -86,7 +86,7 @@ class Camera : MonoBehaviour
var diagnostic = DiagnosticResult.CompilerWarning(ThrowExpressionSuppressor.Rule.SuppressedDiagnosticId)
.WithSeverity(DiagnosticSeverity.Info)
.WithMessage("Null check can be simplified")
.WithLocation(10, 13);
.WithLocation(10, 13);
await VerifyCSharpDiagnosticAsync(test, diagnostic);
}

Просмотреть файл

@ -144,7 +144,7 @@ class Camera : MonoBehaviour
await VerifyCSharpFixAsync(test, fixedTest);
}
[Fact]
public async Task VariableDeclarationNotNullConditionNoMemberAccessOnComponent()
{

Просмотреть файл

@ -262,5 +262,4 @@ class Camera : MonoBehaviour
await VerifyCSharpDiagnosticAsync(context, test);
}
}

Просмотреть файл

@ -10,7 +10,7 @@
<Import Project="$(VstuDirectory)build\UnityVS.props"
Condition="Exists('$(VstuDirectory)build\UnityVS.props')" />
<Import Project="$(VstuDirectory)build\UnityVS.Signing.props"
Condition="Exists('$(VstuDirectory)build\UnityVS.Signing.props')" />

Просмотреть файл

@ -3,7 +3,7 @@
<Import Project="$(VstuDirectory)build\UnityVS.Signing.targets"
Condition="Exists('$(VstuDirectory)build\UnityVS.Signing.targets')" />
<Import Project="$(VstuDirectory)build\Analyzers.targets"
Condition="Exists('$(VstuDirectory)build\Analyzers.targets')" />

Просмотреть файл

@ -73,7 +73,7 @@ public class ImproperMessageCaseAnalyzer : DiagnosticAnalyzer
continue;
if (method.HasPolymorphicModifier())
continue;
continue;
var methodName = method.Identifier.Text;
// We have a valid case match here, so stop further inspection (This will prevent false positive for possible overloads, when one of them is still in the notImplementedMessages lookup)
@ -90,7 +90,7 @@ public class ImproperMessageCaseAnalyzer : DiagnosticAnalyzer
var namedMessages = notImplementedMessages[key];
if (namedMessages.All(m => m.IsStatic != methodSymbol.IsStatic))
continue;
continue;
// We can't use SymbolFinder.FindReferencesAsync() to find possible references, given we do not have access to the solution here yet
context.ReportDiagnostic(Diagnostic.Create(Rule, method.Identifier.GetLocation(), methodName));
@ -126,7 +126,7 @@ public class ImproperMessageCaseCodeFix : CodeFixProvider
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (model == null)
return solution;
var methodSymbol = model.GetDeclaredSymbol(declaration);
if (methodSymbol == null)
return solution;

Просмотреть файл

@ -51,7 +51,7 @@ public class MessageSignatureAnalyzer : DiagnosticAnalyzer
var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration);
if (typeSymbol == null)
return;
var scriptInfo = new ScriptInfo(typeSymbol);
if (!scriptInfo.HasMessages)
return;
@ -76,7 +76,7 @@ public class MessageSignatureAnalyzer : DiagnosticAnalyzer
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method);
if (methodSymbol == null)
continue;
// A message is detected, so the signature is correct
if (scriptInfo.IsMessage(methodSymbol))
continue;

Просмотреть файл

@ -16,7 +16,6 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Unity.Analyzers.Resources;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Unity.Analyzers;

Просмотреть файл

@ -17,7 +17,6 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.Unity.Analyzers.Resources;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Unity.Analyzers;
@ -120,19 +119,19 @@ public class NonGenericGetComponentCodeFix : CodeFixProvider
var argumentSyntax = syntaxList.FirstOrDefault();
if (argumentSyntax == null)
return document;
var typeOf = (TypeOfExpressionSyntax)argumentSyntax.Expression;
var identifierSyntax = (IdentifierNameSyntax)invocation.Expression;
var newArgumentList = invocationArgumentList.RemoveNode(argumentSyntax, SyntaxRemoveOptions.KeepNoTrivia);
if (newArgumentList == null)
return document;
var newInvocation = invocation
.WithExpression(GenericName(
identifierSyntax.Identifier,
TypeArgumentList(
SeparatedList(new[] {typeOf.Type}))))
SeparatedList(new[] { typeOf.Type }))))
.WithArgumentList(newArgumentList);
// If we're casting the GetComponent result, remove the cast as the returned value is now type safe

Просмотреть файл

@ -31,7 +31,7 @@ public class NullableReferenceTypesSuppressor : DiagnosticSuppressor
var syntaxTree = diagnostic.Location.SourceTree;
if (syntaxTree == null)
continue;
var root = syntaxTree.GetRoot();
var node = root.FindNode(diagnostic.Location.SourceSpan);
@ -137,7 +137,7 @@ public class NullableReferenceTypesSuppressor : DiagnosticSuppressor
methodBodies = methodBodies.Concat(methods
.Where(syntax => methodSyntax.DescendantNodes().OfType<InvocationExpressionSyntax>()
.Any(invocationSyntax => invocationSyntax.Expression.ToString() == syntax.Identifier.Text))
.Concat(new[] {methodSyntax})
.Concat(new[] { methodSyntax })
.Select(method => method.Body ?? method.ExpressionBody as SyntaxNode))!;
}

Просмотреть файл

@ -46,7 +46,7 @@ public class PropertyDrawerOnGUIAnalyzer : DiagnosticAnalyzer
{
var invocation = (InvocationExpressionSyntax)context.Node;
var name = invocation.GetMethodNameSyntax();
if (name is not {Identifier.Text: "OnGUI"})
if (name is not { Identifier.Text: "OnGUI" })
return;
var symbol = context.SemanticModel.GetSymbolInfo(invocation);

Просмотреть файл

@ -65,7 +65,7 @@ public class ProtectedUnityMessageAnalyzer : DiagnosticAnalyzer
var symbol = context.SemanticModel.GetDeclaredSymbol(method);
if (symbol == null)
return;
if (!scriptInfo.IsMessage(symbol))
return;

Просмотреть файл

@ -46,6 +46,6 @@ public class SetLocalPositionAndRotationAnalyzer() : BasePositionAndRotationAnal
public class SetLocalPositionAndRotationCodeFix() : BasePositionAndRotationCodeFix(SetLocalPositionAndRotationContext.Instance.Value)
{
protected override string CodeFixTitle => Strings.SetLocalPositionAndRotationCodeFixTitle;
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(SetLocalPositionAndRotationAnalyzer.Rule.Id);
}

Просмотреть файл

@ -45,7 +45,7 @@ public class ThrowExpressionSuppressor : DiagnosticSuppressor
var syntaxTree = diagnostic.Location.SourceTree;
if (syntaxTree == null)
return;
var model = context.GetSemanticModel(syntaxTree);
if (ShouldReportSuppression(binaryExpression.Left, model) || ShouldReportSuppression(binaryExpression.Right, model))

Просмотреть файл

@ -16,7 +16,6 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.Unity.Analyzers.Resources;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Unity.Analyzers;
@ -83,8 +82,8 @@ internal class TryGetComponentContext(string targetIdentifier, bool isVariableDe
var invocationParent = invocation.Parent;
if (invocationParent == null)
return null;
if (!TryGetTargetdentifier(model, invocationParent, out var targetIdentifier, out var isVariableDeclaration))
if (!TryGetTargetdentifier(model, invocationParent, out var targetIdentifier, out var isVariableDeclaration))
return null;
// We want the next line to be an if statement
@ -106,7 +105,7 @@ internal class TryGetComponentContext(string targetIdentifier, bool isVariableDe
if (visitor is BlockSyntax)
break;
if (visitor is IfStatementSyntax {Else: { }})
if (visitor is IfStatementSyntax { Else: { } })
return null;
visitor = visitor.Parent;
@ -143,7 +142,7 @@ internal class TryGetComponentContext(string targetIdentifier, bool isVariableDe
foundIfStatement = null;
conditionIdentifier = null;
if (ifNode is not IfStatementSyntax {Condition: BinaryExpressionSyntax binaryExpression} ifStatement)
if (ifNode is not IfStatementSyntax { Condition: BinaryExpressionSyntax binaryExpression } ifStatement)
return false;
foundBinaryExpression = binaryExpression;
@ -157,8 +156,8 @@ internal class TryGetComponentContext(string targetIdentifier, bool isVariableDe
// We want IdentifierNameSyntax and null as operands
conditionIdentifier = binaryExpression.Left switch
{
IdentifierNameSyntax leftIdentifierName when binaryExpression.Right is LiteralExpressionSyntax {RawKind: (int) SyntaxKind.NullLiteralExpression} => leftIdentifierName.Identifier,
LiteralExpressionSyntax {RawKind: (int) SyntaxKind.NullLiteralExpression} when binaryExpression.Right is IdentifierNameSyntax rightIdentifierName => rightIdentifierName.Identifier,
IdentifierNameSyntax leftIdentifierName when binaryExpression.Right is LiteralExpressionSyntax { RawKind: (int)SyntaxKind.NullLiteralExpression } => leftIdentifierName.Identifier,
LiteralExpressionSyntax { RawKind: (int)SyntaxKind.NullLiteralExpression } when binaryExpression.Right is IdentifierNameSyntax rightIdentifierName => rightIdentifierName.Identifier,
_ => null
};
@ -172,12 +171,12 @@ internal class TryGetComponentContext(string targetIdentifier, bool isVariableDe
switch (invocationParent)
{
case EqualsValueClauseSyntax {Parent: VariableDeclaratorSyntax variableDeclarator}:
case EqualsValueClauseSyntax { Parent: VariableDeclaratorSyntax variableDeclarator }:
isVariableDeclaration = true;
targetIdentifier = variableDeclarator.Identifier;
break;
case AssignmentExpressionSyntax {Left: IdentifierNameSyntax identifierName}:
case AssignmentExpressionSyntax { Left: IdentifierNameSyntax identifierName }:
{
// With an assignment, we want to work only with a field or local variable ('out' constraint)
var symbol = model.GetSymbolInfo(identifierName);
@ -250,7 +249,7 @@ public class TryGetComponentCodeFix : CodeFixProvider
SyntaxNode assignNode = invocation;
while (assignNode.Parent != null && assignNode.Parent is not BlockSyntax)
assignNode = assignNode.Parent;
InvocationExpressionSyntax? newInvocation;
var identifier = Identifier(nameof(UnityEngine.Component.TryGetComponent));
@ -263,7 +262,7 @@ public class TryGetComponentCodeFix : CodeFixProvider
newInvocation = invocation.WithExpression(newNameSyntax);
break;
}
case MemberAccessExpressionSyntax {Name: GenericNameSyntax indirectNameSyntax} memberAccessExpression:
case MemberAccessExpressionSyntax { Name: GenericNameSyntax indirectNameSyntax } memberAccessExpression:
{
var newNameSyntax = indirectNameSyntax.WithIdentifier(identifier);
var newMemberAccessExpression = memberAccessExpression.WithName(newNameSyntax);
@ -281,10 +280,10 @@ public class TryGetComponentCodeFix : CodeFixProvider
true => Argument(
DeclarationExpression(
IdentifierName(
Identifier(TriviaList(),
SyntaxKind.VarKeyword,
"var",
"var",
Identifier(TriviaList(),
SyntaxKind.VarKeyword,
"var",
"var",
TriviaList())),
SingleVariableDesignation(
Identifier(targetIdentifier)))),
@ -318,7 +317,7 @@ public class TryGetComponentCodeFix : CodeFixProvider
var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken);
documentEditor.RemoveNode(assignNode, SyntaxRemoveOptions.KeepNoTrivia);
var ifStatement = context.IfStatement;
var newIfStatement = ifStatement
.WithCondition(newCondition)

Просмотреть файл

@ -81,7 +81,7 @@ public class UnityObjectNullHandlingAnalyzer : DiagnosticAnalyzer
context.RegisterSyntaxNodeAction(AnalyzeCoalesceAssignmentExpression, SyntaxKind.CoalesceAssignmentExpression);
context.RegisterSyntaxNodeAction(AnalyzeIsPatternExpression, SyntaxKind.IsPatternExpression);
}
private static void AnalyzeIsPatternExpression(SyntaxNodeAnalysisContext context)
{
var pattern = (IsPatternExpressionSyntax)context.Node;
@ -89,15 +89,15 @@ public class UnityObjectNullHandlingAnalyzer : DiagnosticAnalyzer
switch (pattern.Pattern)
{
// obj is null
case ConstantPatternSyntax {Expression.RawKind: (int)SyntaxKind.NullLiteralExpression}:
case ConstantPatternSyntax { Expression.RawKind: (int)SyntaxKind.NullLiteralExpression }:
//obj is not null, we need roslyn 3.7.0 here for UnaryPatternSyntax type and SyntaxKind.NotPattern enum value
case UnaryPatternSyntax {RawKind: (int)SyntaxKind.NotPattern, Pattern: ConstantPatternSyntax {Expression.RawKind: (int)SyntaxKind.NullLiteralExpression} }:
case UnaryPatternSyntax { RawKind: (int)SyntaxKind.NotPattern, Pattern: ConstantPatternSyntax { Expression.RawKind: (int)SyntaxKind.NullLiteralExpression } }:
AnalyzeExpression(pattern, pattern.Expression, context, IsPatternRule);
break;
}
}
private static void AnalyzeCoalesceAssignmentExpression(SyntaxNodeAnalysisContext context)
{
var assignment = (AssignmentExpressionSyntax)context.Node;
@ -330,7 +330,7 @@ public class UnityObjectNullHandlingSuppressor : DiagnosticSuppressor
var model = context.GetSemanticModel(invocation.SyntaxTree);
foreach (var argument in invocation.ArgumentList.Arguments)
foreach (var argument in invocation.ArgumentList.Arguments)
{
var typeInfo = model.GetTypeInfo(argument.Expression);
ReportSuppressionOnUnityObject(diagnostic, context, typeInfo.Type);

Просмотреть файл

@ -9,7 +9,6 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Unity.Analyzers.Resources;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Unity.Analyzers;