Skip to content

Commit

Permalink
UtilityAnalyzer: Wrong classifications for AliasQualifiedNames (#7814)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource authored Aug 18, 2023
1 parent 01782bd commit 3c5032e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ name is GenericNameSyntax
} => x == name ? TokenType.TypeName : ClassifyIdentifierByModel(name),
// Walk up classified names (to detect namespace and using context)
QualifiedNameSyntax parent => ClassifySimpleNameTypeSpecialContext(parent, name),
AliasQualifiedNameSyntax parent => ClassifySimpleNameTypeSpecialContext(parent, name),
// We are in a "normal" type context like a declaration
_ => ClassifySimpleNameTypeInTypeContext(name),
};
Expand All @@ -220,14 +221,17 @@ private TokenType ClassifySimpleNameTypeInTypeContext(SimpleNameSyntax name) =>
name switch
{
// unqualified types called "var" or "dynamic" are classified as keywords.
{ Parent: not QualifiedNameSyntax } => name is { Identifier.Text: "var" or "dynamic" }
{ Parent: not QualifiedNameSyntax and not AliasQualifiedNameSyntax } => name is { Identifier.Text: "var" or "dynamic" }
? TokenType.Keyword
: TokenType.TypeName,
{ Parent: QualifiedNameSyntax { Parent: { } parentOfTopMostQualifiedName, Right: { } right } topMostQualifiedName } when
right == name // On the right hand side?
&& parentOfTopMostQualifiedName is not QualifiedNameSyntax // Is this the most right hand side?
// This is a type, except on the right side of "is" where it might also be a constant like Int32.MaxValue

// This is a type, except on the right side of "is" where it might also be a constant like Int32.MaxValue
&& !NameIsRightOfIsExpression(topMostQualifiedName, parentOfTopMostQualifiedName) => TokenType.TypeName,
// Name is directly after alias global::SomeType
{ Parent: AliasQualifiedNameSyntax { Name: { } x, Parent: not QualifiedNameSyntax } } when name == x => TokenType.TypeName,
// We are somewhere in a qualified name. It probably is a namespace but could also be the outer type of a nested type.
_ => ClassifyIdentifierByModel(name),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,13 @@ public class Test {

[DataTestMethod]
[DataRow("using [u:System];", false)]
[DataRow("using [u:System].[u:Collections].[u:Generic];", false)]
[DataRow("using [k:global]::[u:System].[u:Collections].[u:Generic];", false)]
[DataRow("using [t:X] = System.Math;", true)]
[DataRow("using X = [u:System].Math;", true)]
[DataRow("using X = System.[t:Math];", true)]
[DataRow("using X = [t:InGlobalNamespace];", true)]
[DataRow("using X = [k:global]::[t:InGlobalNamespace];", true)]
[DataRow("using [u:X] = System.Collections;", true)]
[DataRow("using X = [u:System].Collections;", true)]
[DataRow("using X = System.[u:Collections];", true)]
Expand All @@ -238,8 +242,12 @@ public class Test {
#if NET
[DataRow("using [u:System].[u:Buffers];", false)]
#endif
public void IdentifierToken_Usings(string syntax, bool allowSemanticModel = true) =>
ClassifierTestHarness.AssertTokenTypes(syntax, allowSemanticModel);
public void IdentifierToken_Usings(string usings, bool allowSemanticModel = true) =>
ClassifierTestHarness.AssertTokenTypes($$"""
{{usings}}
class InGlobalNamespace { }
""", allowSemanticModel);

[DataTestMethod]
[DataRow("namespace [u:A] { }", false)]
Expand Down Expand Up @@ -837,6 +845,20 @@ public class Test
}
""", allowSemanticModel);
[DataTestMethod]
[DataRow("[k:global]::[t:Test]", false)]
[DataRow("[k:global]::[u:System].Int32", true)]
[DataRow("[k:global]::System.[t:Int32]", false)]
public void IdentifierToken_Type_Global(string typeName, bool allowSemanticModel = true) =>
ClassifierTestHarness.AssertTokenTypes($$"""
using System;
public class Test
{
{{typeName}} M() => default;
}
""", allowSemanticModel);
[DataTestMethod]
[DataRow("[u:aInstance]", false)] // Some simple identifier syntax in an ordinary expression context must be boud to a field/property/local or something else that produces a value, but it can not be a type
[DataRow("aInstance.InstanceProp.[u:InstanceProp]", false)] // Most right can not be a type in an ordinary expression context
Expand Down

0 comments on commit 3c5032e

Please sign in to comment.