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

Skip to content

Commit 2869451

Browse files
committed
C#: Use pattern matching
1 parent 155453d commit 2869451

4 files changed

Lines changed: 14 additions & 14 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable<Param
3030
foreach (var param in @params)
3131
VisitParameter(param);
3232

33-
if (body is ExpressionSyntax)
34-
Create(cx, (ExpressionSyntax)body, this, 0);
35-
else if (body is BlockSyntax)
36-
Statements.Block.Create(cx, (BlockSyntax)body, this, 0);
33+
if (body is ExpressionSyntax exprBody)
34+
Create(cx, exprBody, this, 0);
35+
else if (body is BlockSyntax blockBody)
36+
Statements.Block.Create(cx, blockBody, this, 0);
3737
else
3838
cx.ModelError(body, "Unhandled lambda body");
3939
});

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,18 @@ protected void PopulateType(TextWriter trapFile, bool constructUnderlyingTupleTy
133133
trapFile.parent_namespace(this, Namespace.Create(Context, symbol.ContainingNamespace));
134134
}
135135

136-
if (symbol is IArrayTypeSymbol)
136+
if (symbol is IArrayTypeSymbol array)
137137
{
138138
// They are in the namespace of the original object
139-
var elementType = ((IArrayTypeSymbol)symbol).ElementType;
139+
var elementType = array.ElementType;
140140
var ns = elementType.TypeKind == TypeKind.TypeParameter ? Context.Compilation.GlobalNamespace : elementType.ContainingNamespace;
141141
if (ns != null)
142142
trapFile.parent_namespace(this, Namespace.Create(Context, ns));
143143
}
144144

145-
if (symbol is IPointerTypeSymbol)
145+
if (symbol is IPointerTypeSymbol pointer)
146146
{
147-
var elementType = ((IPointerTypeSymbol)symbol).PointedAtType;
147+
var elementType = pointer.PointedAtType;
148148
var ns = elementType.TypeKind == TypeKind.TypeParameter ? Context.Compilation.GlobalNamespace : elementType.ContainingNamespace;
149149

150150
if (ns != null)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,11 @@ public static bool IsUnboundNullable(this ITypeSymbol type) =>
456456
/// <returns>The list of parameters, or an empty list.</returns>
457457
public static IEnumerable<IParameterSymbol> GetParameters(this ISymbol parameterizable)
458458
{
459-
if (parameterizable is IMethodSymbol)
460-
return ((IMethodSymbol)parameterizable).Parameters;
459+
if (parameterizable is IMethodSymbol meth)
460+
return meth.Parameters;
461461

462-
if (parameterizable is IPropertySymbol)
463-
return ((IPropertySymbol)parameterizable).Parameters;
462+
if (parameterizable is IPropertySymbol prop)
463+
return prop.Parameters;
464464

465465
return Enumerable.Empty<IParameterSymbol>();
466466
}

csharp/extractor/Semmle.Extraction/Id.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public Key(params object[] args)
5858
TrapBuilder = new StringWriter();
5959
foreach (var arg in args)
6060
{
61-
if (arg is IEntity)
61+
if (arg is IEntity entity)
6262
{
63-
var key = ((IEntity)arg).Label;
63+
var key = entity.Label;
6464
TrapBuilder.Write("{#");
6565
TrapBuilder.Write(key.Value.ToString());
6666
TrapBuilder.Write("}");

0 commit comments

Comments
 (0)