C#: Treat _ as an ordinary variable in a foreach.

This commit is contained in:
Calum Grant 2019-07-04 16:29:36 +01:00
Родитель 8aeeec01ff
Коммит 0e62377dd2
4 изменённых файлов: 40 добавлений и 18 удалений

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

@ -26,10 +26,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements
var location = cx.Create(Stmt.Identifier.GetLocation());
if (typeSymbol.Name != "_")
Expressions.VariableDeclaration.Create(cx, typeSymbol, type, Stmt.Type, location, location, Stmt.Type.IsVar, this, 0);
else
TypeMention.Create(cx, Stmt.Type, this, type);
Expressions.VariableDeclaration.Create(cx, typeSymbol, type, Stmt.Type, location, location, Stmt.Type.IsVar, this, 0);
Statement.Create(cx, Stmt.Statement, this, 2);
}

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

@ -22,16 +22,30 @@ namespace Semmle.Extraction.CSharp.Entities
Loc = loc;
}
static TypeSyntax GetArrayElementType(ArrayTypeSyntax array)
static TypeSyntax GetElementType(TypeSyntax type)
{
return array.ElementType is ArrayTypeSyntax a ?
GetArrayElementType(a) :
array.ElementType;
switch(type)
{
case ArrayTypeSyntax ats:
return GetElementType(ats.ElementType);
case NullableTypeSyntax nts:
return GetElementType(nts.ElementType);
default:
return type;
}
}
static Type GetArrayElementType(Type t)
static Type GetElementType(Type t)
{
return t is ArrayType a ? GetArrayElementType(a.ElementType.Type) : t;
switch(t)
{
case ArrayType at:
return GetElementType(at.ElementType.Type);
case NamedType nt when nt.symbol.IsBoundNullable():
return nt.TypeArguments.Single();
default:
return t;
}
}
void Populate()
@ -39,10 +53,8 @@ namespace Semmle.Extraction.CSharp.Entities
switch (Syntax.Kind())
{
case SyntaxKind.ArrayType:
var ats = (ArrayTypeSyntax)Syntax;
var at = (ArrayType)Type;
Emit(Loc ?? Syntax.GetLocation(), Parent, Type);
Create(cx, GetArrayElementType(ats), this, GetArrayElementType(at));
Create(cx, GetElementType(Syntax), this, GetElementType(Type));
return;
case SyntaxKind.NullableType:
var nts = (NullableTypeSyntax)Syntax;

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

@ -161,7 +161,7 @@ namespace NoPia
unsafe class ArrayTypesTest
{
int*[][] field;
int*[][] field;
}
class NameofNamespace
@ -169,4 +169,15 @@ class NameofNamespace
string s = nameof(System);
}
// semmle-extractor-options: /r:System.Dynamic.Runtime.dll
class UsingDiscard
{
void F()
{
foreach(var _ in new IDisposable[] { })
using(_)
{
}
}
}
// semmle-extractor-options: /r:System.Dynamic.Runtime.dll /langversion:8.0

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

@ -76,7 +76,9 @@
| Program.cs:157:31:157:50 | EmbeddedTypesManager<,> |
| Program.cs:157:52:157:72 | TEmbeddedTypesManager |
| Program.cs:157:75:157:87 | TEmbeddedType |
| Program.cs:164:3:164:5 | Int32 |
| Program.cs:164:3:164:6 | Int32* |
| Program.cs:164:3:164:10 | Int32*[][] |
| Program.cs:164:5:164:7 | Int32 |
| Program.cs:164:5:164:8 | Int32* |
| Program.cs:164:5:164:12 | Int32*[][] |
| Program.cs:169:5:169:10 | String |
| Program.cs:174:5:174:8 | Void |
| Program.cs:176:17:176:19 | IDisposable |