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 S4056: Rule should not recommend to use obsolete methods #772

Merged
merged 6 commits into from
Sep 13, 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 @@ -2136,19 +2136,6 @@
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\Nancy\src\Nancy\Response.cs",
"region": {
"startLine": 171,
"startColumn": 58,
"endLine": 171,
"endColumn": 84
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\Nancy\src\Nancy\ResponseExtensions.cs",
"region": {
"startLine": 203,
Expand Down Expand Up @@ -2695,19 +2682,6 @@
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\Nancy\src\Nancy\Security\CsrfValidationException.cs",
"region": {
"startLine": 10,
"startColumn": 20,
"endLine": 10,
"endColumn": 37
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\Nancy\src\Nancy\Security\SSLProxy.cs",
"region": {
"startLine": 34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\Nancy\src\Nancy.Hosting.Self.Tests\NancySelfHostFixture.cs",
"region": {
"startLine": 235,
"startColumn": 25,
"endLine": 235,
"endColumn": 51
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\Nancy\src\Nancy.Tests\ShouldExtensions.cs",
"region": {
"startLine": 15,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,19 +771,6 @@
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Dispatch\ForkJoinDispatcher.cs",
"region": {
"startLine": 29,
"startColumn": 98,
"endLine": 29,
"endColumn": 130
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Dispatch\Mailbox.cs",
"region": {
"startLine": 27,
Expand All @@ -810,19 +797,6 @@
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Dispatch\SingleThreadDispatcher.cs",
"region": {
"startLine": 39,
"startColumn": 25,
"endLine": 39,
"endColumn": 57
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Dispatch\ThreadPoolBuilder.cs",
"region": {
"startLine": 31,
Expand All @@ -838,19 +812,6 @@
"location": {
"uri": "sources\akka.net\src\core\Akka\Dispatch\ThreadPoolBuilder.cs",
"region": {
"startLine": 31,
"startColumn": 47,
"endLine": 31,
"endColumn": 79
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Dispatch\ThreadPoolBuilder.cs",
"region": {
"startLine": 38,
"startColumn": 20,
"endLine": 38,
Expand Down Expand Up @@ -916,19 +877,6 @@
"location": {
"uri": "sources\akka.net\src\core\Akka\Event\Error.cs",
"region": {
"startLine": 53,
"startColumn": 17,
"endLine": 53,
"endColumn": 38
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Event\Error.cs",
"region": {
"startLine": 54,
"startColumn": 17,
"endLine": 54,
Expand Down Expand Up @@ -1047,19 +995,6 @@
"uri": "sources\akka.net\src\core\Akka\Event\LogEvent.cs",
"region": {
"startLine": 70,
"startColumn": 69,
"endLine": 70,
"endColumn": 90
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka\Event\LogEvent.cs",
"region": {
"startLine": 70,
"startColumn": 143,
"endLine": 70,
"endColumn": 176
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,6 @@
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Sinks\Messages.cs",
"region": {
"startLine": 123,
"startColumn": 17,
"endLine": 123,
"endColumn": 33
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Sinks\Messages.cs",
"region": {
"startLine": 151,
"startColumn": 20,
"endLine": 153,
Expand All @@ -355,19 +342,6 @@
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Sinks\Messages.cs",
"region": {
"startLine": 152,
"startColumn": 17,
"endLine": 152,
"endColumn": 33
}
}
},
{
"id": "S4056",
"message": "Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner.Shared\Sinks\MessageSink.cs",
"region": {
"startLine": 151,
Expand Down
4 changes: 3 additions & 1 deletion sonaranalyzer-dotnet/rspec/cs/S4056_c#.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ <h2>Compliant Solution</h2>
}
}
</pre>

<h2>Exceptions</h2>
<p>This rule will not raise an issue when the overload is marked as obsolete.</p>

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private static bool HasOverloadWithStringComparison(ExpressionSyntax expression,
{
return semanticModel.GetMemberGroup(expression)
.OfType<IMethodSymbol>()
.Where(m => !IsObsolete(m))
Copy link
Contributor

Choose a reason for hiding this comment

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

Smart idea! I would have ignored only the method the guy was mentioning!

.Any(HasAnyCheckedTypeParameter);
}

Expand All @@ -87,7 +88,12 @@ private static bool IsInvalidCall(ExpressionSyntax expression, SemanticModel sem
!HasAnyCheckedTypeParameter(methodSymbol);
}

private static bool HasAnyCheckedTypeParameter(IMethodSymbol method)
private static bool IsObsolete(ISymbol method)
{
return method.GetAttributes().Any(a => a.AttributeClass.Is(KnownType.System_ObsoleteAttribute));
}

private static bool HasAnyCheckedTypeParameter(ISymbol method)
{
return method.GetParameters().Any(parameter => parameter.Type.IsAny(checkedTypes));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ <h2>Compliant Solution</h2>
}
}
</pre>

<h2>Exceptions</h2>
<p>This rule will not raise an issue when the overload is marked as obsolete.</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@

namespace Tests.Diagnostics
{
static class Methods
{
public static int DoStuff(string foo) => 1;

[Obsolete]
public static int DoStuff(string foo, IFormatProvider format) => 1;


public static int DoStuff2(string foo) => 1;

[Obsolete]
public static int DoStuff2(string foo, IFormatProvider format) => 1;
public static int DoStuff2(string foo, CultureInfo foo2) => 1;
}

class Program
{
enum Colors { Red, Green, Blue, Yellow = 12 };

void InvalidCases()
{
42.ToString(); // Noncompliant {{Use the overload that takes a 'CultureInfo' or 'IFormatProvider' parameter.}}
// ^^^^^^^^^^^^^
string.Format("bla"); // Noncompliant
Methods.DoStuff2("foo"); // Noncompliant
}

void ValidCases()
Expand All @@ -23,6 +41,11 @@ void ValidCases()
var resourceManager = new ResourceManager(typeof(Program));
resourceManager.GetObject("a"); // Compliant - excluded
resourceManager.GetString("a"); // Compliant - excluded

Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));
Colors myColor = Colors.Yellow;
Console.WriteLine("Colors.Red = {0}", myColor.ToString("d"));
Methods.DoStuff("foo");
}
}
}