提交 af35b152 编写于 作者: G Gen Lu

Fix NRE in "Literal.Text" when the constant value is null.

上级 16faf273
......@@ -1560,5 +1560,43 @@ public void M0(C p)
Diagnostic(ConditionalAccessOperationTestAnalyzer.ConditionalAccessOperationDescriptor.Id, "Field1?.M0(null)").WithLocation(32, 9),
Diagnostic(ConditionalAccessOperationTestAnalyzer.ConditionalAccessInstanceOperationDescriptor.Id, "Field1").WithLocation(32, 9));
}
[WorkItem(9116, "https://github.com/dotnet/roslyn/issues/9116")]
[Fact]
public void LiteralCSharp()
{
const string source = @"
public class A
{
public void M()
{
object a = null;
}
}
struct S
{
void M(
int i = 1,
string str = ""hello"",
object o = null,
S s = default(S))
{
M();
}
}
";
CreateCompilationWithMscorlib45(source)
.VerifyDiagnostics(Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "a").WithArguments("a").WithLocation(6, 16))
.VerifyAnalyzerDiagnostics(new DiagnosticAnalyzer[] { new LiteralTestAnalyzer() }, null, null, false,
Diagnostic("Literal", "null").WithArguments("null").WithLocation(6, 20),
Diagnostic("Literal", "1").WithArguments("1").WithLocation(13, 17),
Diagnostic("Literal", @"""hello""").WithArguments(@"""hello""").WithLocation(14, 22),
Diagnostic("Literal", "null").WithArguments("null").WithLocation(15, 20),
Diagnostic("Literal", "M()").WithArguments("1").WithLocation(18, 9),
Diagnostic("Literal", "M()").WithArguments(@"""hello""").WithLocation(18, 9),
Diagnostic("Literal", "M()").WithArguments("null").WithLocation(18, 9),
Diagnostic("Literal", "M()").WithArguments("null").WithLocation(18, 9));
}
}
}
\ No newline at end of file
......@@ -1773,4 +1773,33 @@ public sealed override void Initialize(AnalysisContext context)
OperationKind.LoopStatement);
}
}
public class LiteralTestAnalyzer : DiagnosticAnalyzer
{
private const string ReliabilityCategory = "Reliability";
public static readonly DiagnosticDescriptor LiteralDescriptor = new DiagnosticDescriptor(
"Literal",
"A literal is found",
"A literal of value {0} is found",
ReliabilityCategory,
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(LiteralDescriptor); }
}
public sealed override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(
(operationContext) =>
{
var literal = (ILiteralExpression)operationContext.Operation;
operationContext.ReportDiagnostic(Diagnostic.Create(LiteralDescriptor, literal.Syntax.GetLocation(), literal.Text));
},
OperationKind.LiteralExpression);
}
}
}
\ No newline at end of file
......@@ -308,7 +308,7 @@ public Literal(ConstantValue value, ITypeSymbol resultType, SyntaxNode syntax)
this.Syntax = syntax;
}
public string Text => _value.Value.ToString();
public string Text => _value.GetValueToDisplay();
public ITypeSymbol Type { get; }
......
......@@ -193,7 +193,7 @@ public override void VisitEndStatement(IEndStatement operation)
public override void VisitInvocationExpression(IInvocationExpression operation)
{
Visit(operation.Instance);
VisitArray(operation.ArgumentsInSourceOrder);
VisitArray(operation.ArgumentsInParameterOrder);
}
public override void VisitArgument(IArgument operation)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册