Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S2275: InvalidCastExteption when analyzing single argument method #303

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the test for all methods?


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