Skip to content

Commit 00b5184

Browse files
authored
use null propagation and mark as warning in editorconfig (#5383)
1 parent 5017ae8 commit 00b5184

File tree

5 files changed

+6
-25
lines changed

5 files changed

+6
-25
lines changed

src/Analyzers/MSTest.Analyzers.CodeFixes/AvoidExpectedExceptionAttributeFixer.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
5959
}
6060

6161
IMethodSymbol? methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken);
62-
if (methodSymbol is null)
63-
{
64-
return;
65-
}
6662

67-
AttributeData? attribute = methodSymbol.GetAttributes().FirstOrDefault(
63+
AttributeData? attribute = methodSymbol?.GetAttributes().FirstOrDefault(
6864
attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, expectedExceptionAttributeSymbol));
6965

7066
if (attribute?.ApplicationSyntaxReference is not { } syntaxRef)

src/Analyzers/MSTest.Analyzers.CodeFixes/PreferConstructorOverTestInitializeFixer.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4343
TextSpan diagnosticSpan = diagnostic.Location.SourceSpan;
4444

4545
SyntaxToken syntaxToken = root.FindToken(diagnosticSpan.Start);
46-
if (syntaxToken.Parent is null)
47-
{
48-
return;
49-
}
5046

5147
// Find the method declaration identified by the diagnostic.
52-
MethodDeclarationSyntax? methodDeclaration = syntaxToken.Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().FirstOrDefault();
48+
MethodDeclarationSyntax? methodDeclaration = syntaxToken.Parent?.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().FirstOrDefault();
5349
if (methodDeclaration == null)
5450
{
5551
return;

src/Analyzers/MSTest.Analyzers.CodeFixes/PreferDisposeOverTestCleanupFixer.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4444
TextSpan diagnosticSpan = diagnostic.Location.SourceSpan;
4545

4646
SyntaxToken syntaxToken = root.FindToken(diagnosticSpan.Start);
47-
if (syntaxToken.Parent is null)
48-
{
49-
return;
50-
}
5147

5248
// Find the TestCleanup method declaration identified by the diagnostic.
53-
MethodDeclarationSyntax? testCleanupMethod = syntaxToken.Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().FirstOrDefault();
49+
MethodDeclarationSyntax? testCleanupMethod = syntaxToken.Parent?.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().FirstOrDefault();
5450
if (testCleanupMethod == null ||
5551
!IsTestCleanupMethodValid(testCleanupMethod) ||
5652
testCleanupMethod.Parent is not TypeDeclarationSyntax containingType)

src/Analyzers/MSTest.Analyzers.CodeFixes/PreferTestInitializeOverConstructorFixer.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4343
TextSpan diagnosticSpan = diagnostic.Location.SourceSpan;
4444

4545
SyntaxToken syntaxToken = root.FindToken(diagnosticSpan.Start);
46-
if (syntaxToken.Parent is null)
47-
{
48-
return;
49-
}
5046

5147
// Find the constructor declaration identified by the diagnostic.
52-
ConstructorDeclarationSyntax? constructorDeclaration = syntaxToken.Parent.AncestorsAndSelf().OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
48+
ConstructorDeclarationSyntax? constructorDeclaration = syntaxToken.Parent?.AncestorsAndSelf().OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
5349
if (constructorDeclaration == null)
5450
{
5551
return;

src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/PassiveNode.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,9 @@ public async Task SendAttachmentsAsync(TestsAttachments testsAttachments, Cancel
102102

103103
public void Dispose()
104104
{
105-
if (_messageHandler != null)
105+
if (_messageHandler is IDisposable disposable)
106106
{
107-
if (_messageHandler is IDisposable disposable)
108-
{
109-
disposable.Dispose();
110-
}
107+
disposable.Dispose();
111108
}
112109
}
113110
}

0 commit comments

Comments
 (0)