Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e208f3d

Browse files
committed
C#: Simplify null checks with pattern matching, ??, and ?:
1 parent 504f56a commit e208f3d

21 files changed

Lines changed: 57 additions & 76 deletions

File tree

csharp/autobuilder/Semmle.Autobuild.Shared/MsBuildRule.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,9 @@ BuildScript GetNugetRestoreScript() =>
9898

9999
command.Argument("/p:UseSharedCompilation=false");
100100

101-
string target = builder.Options.MsBuildTarget != null
102-
? builder.Options.MsBuildTarget
103-
: "rebuild";
104-
string? platform = builder.Options.MsBuildPlatform != null
105-
? builder.Options.MsBuildPlatform
106-
: projectOrSolution is ISolution s1 ? s1.DefaultPlatformName : null;
107-
string? configuration = builder.Options.MsBuildConfiguration != null
108-
? builder.Options.MsBuildConfiguration
109-
: projectOrSolution is ISolution s2 ? s2.DefaultConfigurationName : null;
101+
string target = builder.Options.MsBuildTarget ?? "rebuild";
102+
string? platform = builder.Options.MsBuildPlatform ?? (projectOrSolution is ISolution s1 ? s1.DefaultPlatformName : null);
103+
string? configuration = builder.Options.MsBuildConfiguration ?? (projectOrSolution is ISolution s2 ? s2.DefaultConfigurationName : null);
110104

111105
command.Argument("/t:" + target);
112106
if (platform != null)

csharp/extractor/Semmle.Extraction.CIL/Context.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public Entities.Type ErrorType
113113
/// <returns>The debugging information, or null if the information could not be located.</returns>
114114
public PDB.IMethod? GetMethodDebugInformation(MethodDefinitionHandle handle)
115115
{
116-
return pdb == null ? null : pdb.GetMethod(handle.ToDebugInformationHandle());
116+
return pdb?.GetMethod(handle.ToDebugInformationHandle());
117117
}
118118
}
119119

csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ public MemberReferenceMethod(GenericContext gc, MemberReferenceHandle handle) :
409409

410410
parent = (GenericContext)cx.CreateGeneric(gc, mr.Parent);
411411

412-
var parentMethod = parent as Method;
413-
414-
var declType = parentMethod is null ? parent as Type : parentMethod.DeclaringType;
412+
var declType = parent is Method parentMethod
413+
? parentMethod.DeclaringType
414+
: parent as Type;
415415

416416
if (declType is null)
417417
throw new InternalError("Parent context of method is not a type");

csharp/extractor/Semmle.Extraction.CIL/PDB/NativePdbReader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public Document(ISymUnmanagedDocument doc)
3939

4040
public override bool Equals(object? obj)
4141
{
42-
var otherDoc = obj as Document;
43-
return otherDoc != null && Path.Equals(otherDoc.Path);
42+
return obj is Document otherDoc && Path.Equals(otherDoc.Path);
4443
}
4544

4645
public override int GetHashCode() => Path.GetHashCode();

csharp/extractor/Semmle.Extraction.CIL/PDB/PdbReader.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public override string ToString()
5656

5757
public override bool Equals(object? obj)
5858
{
59-
var otherLocation = obj as Location;
60-
61-
return otherLocation != null &&
59+
return obj is Location otherLocation &&
6260
File.Equals(otherLocation.File) &&
6361
StartLine == otherLocation.StartLine &&
6462
StartColumn == otherLocation.StartColumn &&

csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,8 @@ void DoAnalyseAssembly(PortableExecutableReference r)
290290
// in c to be garbage collected.
291291
Compilation c = compilation.Clone();
292292

293-
var assembly = c.GetAssemblyOrModuleSymbol(r) as IAssemblySymbol;
294293

295-
if (assembly != null)
294+
if (c.GetAssemblyOrModuleSymbol(r) is IAssemblySymbol assembly)
296295
{
297296
var cx = extractor.CreateContext(c, trapWriter, new AssemblyScope(assembly, assemblyPath, false), AddAssemblyTrapPrefix);
298297

csharp/extractor/Semmle.Extraction.CSharp/Entities/Accessor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ protected Accessor(Context cx, IMethodSymbol init)
1616
public static IPropertySymbol GetPropertySymbol(IMethodSymbol symbol)
1717
{
1818
// Usually, the property/indexer can be fetched from the associated symbol
19-
var prop = symbol.AssociatedSymbol as IPropertySymbol;
20-
if (prop != null)
19+
if (symbol.AssociatedSymbol is IPropertySymbol prop)
2120
return prop;
2221

2322
// But for properties/indexers that implement explicit interfaces, Roslyn

csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override void ExtractInitializers(TextWriter trapFile)
3636
if (!IsSourceDeclaration) return;
3737

3838
var syntax = Syntax;
39-
var initializer = syntax == null ? null : syntax.Initializer;
39+
var initializer = syntax?.Initializer;
4040

4141
if (initializer == null) return;
4242

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ protected static ExpressionSyntax FindConditionalQualifier(ExpressionSyntax node
212212
{
213213
for (SyntaxNode n = node; n != null; n = n.Parent)
214214
{
215-
var conditionalAccess = n.Parent as ConditionalAccessExpressionSyntax;
216-
217-
if (conditionalAccess != null && conditionalAccess.WhenNotNull == n)
215+
if (n.Parent is ConditionalAccessExpressionSyntax conditionalAccess &&
216+
conditionalAccess.WhenNotNull == n)
217+
{
218218
return conditionalAccess.Expression;
219+
}
219220
}
220221

221222
throw new InternalError(node, "Unable to locate a ConditionalAccessExpression");
@@ -434,8 +435,7 @@ public AnnotatedTypeSymbol ExpressionType
434435
// Clearly a bug.
435436
if (type.Symbol?.TypeKind == Microsoft.CodeAnalysis.TypeKind.Error)
436437
{
437-
var arrayCreation = Node as ArrayCreationExpressionSyntax;
438-
if (arrayCreation != null)
438+
if (Node is ArrayCreationExpressionSyntax arrayCreation)
439439
{
440440
var elementType = Context.GetType(arrayCreation.Type.ElementType);
441441

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ElementAccess.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ protected override void PopulateExpression(TextWriter trapFile)
3939

4040
var symbolInfo = cx.GetSymbolInfo(base.Syntax);
4141

42-
var indexer = symbolInfo.Symbol as IPropertySymbol;
43-
if (indexer != null)
42+
if (symbolInfo.Symbol is IPropertySymbol indexer)
4443
{
4544
trapFile.expr_access(this, Indexer.Create(cx, indexer));
4645
}

0 commit comments

Comments
 (0)