Skip to content

Commit

Permalink
Fix S2275: InvalidCastExteption when analyzing single argument method
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeri Hristov committed May 16, 2017
1 parent 3cb1efc commit 28012a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static void CheckForFormatStringIssues(SyntaxNodeAnalysisContext analysi
return;
}

var failure = TryParseAndValidate((string)constValue.Value, invocation.ArgumentList,
var failure = TryParseAndValidate(constValue.Value as string, invocation.ArgumentList,
formatArgumentIndex, analysisContext.SemanticModel);
if (failure == null ||
CanIgnoreFailure(failure, currentMethodSignature.Name, invocation.ArgumentList.Arguments.Count))
Expand Down Expand Up @@ -150,8 +150,10 @@ private static bool CanIgnoreFailure(ValidationFailure failure, string methodNam
failure == ValidationFailure.FormatItemIndexTooHigh)
{
return false;
}

}

// All methods in HandledFormatMethods that do not end on Format have an overload
// with only one argument and the rule should not raise an issue
return argumentsCount == 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void System_Console_Write(string[] args)
Console.Write("{0}", 42);
Console.Write("{{}}"); // Compliant, displays {}
Console.Write("{"); // Compliant
Console.Write(ulong.MaxValue);

Console.Write("[0}", args[0]); // Noncompliant
Console.Write("{-1}", args[0]); // Noncompliant
Expand All @@ -80,6 +81,7 @@ void System_Console_WriteLine(string[] args)
Console.WriteLine("0");
Console.WriteLine("{0}", 42);
Console.WriteLine("{"); // Compliant
Console.WriteLine(ulong.MaxValue);

Console.WriteLine("[0}", args[0]); // Noncompliant
Console.WriteLine("{-1}", args[0]); // Noncompliant
Expand All @@ -102,6 +104,7 @@ void System_IO_TextWriter_Write(string[] args)
System.IO.TextWriter.Write("0");
System.IO.TextWriter.Write("{0}", 42);
System.IO.TextWriter.Write("{"); // Compliant
System.IO.TextWriter.Write(ulong.MaxValue);

System.IO.TextWriter.Write("[0}", args[0]); // Noncompliant
System.IO.TextWriter.Write("{-1}", args[0]); // Noncompliant
Expand All @@ -114,6 +117,7 @@ void System_IO_TextWriter_WriteLine(string[] args)
System.IO.TextWriter.WriteLine("0");
System.IO.TextWriter.WriteLine("{0}", 42);
System.IO.TextWriter.WriteLine("{"); // Compliant
System.IO.TextWriter.WriteLine(ulong.MaxValue);

System.IO.TextWriter.WriteLine("[0}", args[0]); // Noncompliant
System.IO.TextWriter.WriteLine("{-1}", args[0]); // Noncompliant
Expand All @@ -126,6 +130,7 @@ void System_Diagnostics_Debug_WriteLine(string[] args)
System.Diagnostics.Debug.WriteLine("0");
System.Diagnostics.Debug.WriteLine("{0}", 42);
System.Diagnostics.Debug.WriteLine("{"); // Compliant
System.Diagnostics.Debug.WriteLine(ulong.MaxValue);

System.Diagnostics.Debug.WriteLine("[0}", args[0]); // Noncompliant
System.Diagnostics.Debug.WriteLine("{-1}", args[0]); // Noncompliant
Expand Down Expand Up @@ -173,7 +178,7 @@ void System_Diagnostics_TraceSource_TraceInformation(string[] args)
{
System.Diagnostics.TraceSource.TraceInformation("0");
System.Diagnostics.TraceSource.TraceInformation("{0}", 42);
System.Diagnostics.TraceSource.TraceInformation( "{"); // Compliant
System.Diagnostics.TraceSource.TraceInformation("{"); // Compliant

System.Diagnostics.TraceSource.TraceInformation("[0}", args[0]); // Noncompliant
System.Diagnostics.TraceSource.TraceInformation("{-1}", args[0]); // Noncompliant
Expand Down

0 comments on commit 28012a9

Please sign in to comment.