rspec/rules/S1128/csharp/rule.adoc

104 lines
3.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-10-10 16:26:46 +02:00
Unnecessary `using` directives refer to importing namespaces, types or creating aliases that are not used or referenced anywhere in the code.
2023-10-10 16:26:46 +02:00
include::../description.adoc[]
2020-06-30 12:47:33 +02:00
2023-10-10 16:26:46 +02:00
Starting with C# 10, it's possible to define https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#global-modifier[global usings] for an entire project. They reduce the need for repetitive namespace inclusions, but can also mask which namespaces are truly necessary for the code at hand. Over-relying on them can lead to less transparent code dependencies, especially for newcomers to the project.
=== Exceptions
The rule will not raise a warning for `global using` directives, even if none of the types of that namespace are used in the project:
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
2023-10-10 16:26:46 +02:00
global using System.Net.Sockets; // Compliant by exception
----
Unnecessary `using` directives are ignored in ASP.NET Core projects in the following files:
* ``++_Imports.razor++``
* ``++_ViewImports.cshtml++``
2023-10-10 16:26:46 +02:00
== How to fix it
2020-06-30 12:47:33 +02:00
2023-10-10 16:26:46 +02:00
While it's not difficult to remove these unneeded lines manually, modern code editors support the removal of every unnecessary `using` directive with a single click from every file of the project.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
using System.IO;
using System.Linq;
using System.Collections.Generic; // Noncompliant - no types are used from this namespace
using MyApp.Helpers; // Noncompliant - FileHelper is in the same namespace
using MyCustomNamespace; // Noncompliant - no types are used from this namespace
namespace MyApp.Helpers
2020-06-30 12:47:33 +02:00
{
2023-10-10 16:26:46 +02:00
public class FileHelper
2020-06-30 12:47:33 +02:00
{
2023-10-10 16:26:46 +02:00
public static string ReadFirstLine(string filePath) =>
File.ReadAllLines(filePath).First();
2020-06-30 12:47:33 +02:00
}
}
----
2023-10-10 16:26:46 +02:00
==== Compliant solution
2020-06-30 12:47:33 +02:00
2023-10-10 16:26:46 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
using System.IO;
2023-10-10 16:26:46 +02:00
using System.Linq;
2020-06-30 12:47:33 +02:00
2023-10-10 16:26:46 +02:00
namespace MyApp.Helpers
2020-06-30 12:47:33 +02:00
{
2023-10-10 16:26:46 +02:00
public class FileHelper
2020-06-30 12:47:33 +02:00
{
2023-10-10 16:26:46 +02:00
public static string ReadFirstLine(string filePath) =>
File.ReadAllLines(filePath).First();
2020-06-30 12:47:33 +02:00
}
}
----
2023-10-10 16:26:46 +02:00
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive[MSDN - using directives]
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/namespace[MSDN - namespaces]
=== Related rules
* S1144 - Unused private types or members should be removed
* S1481 - Unused local variables should be removed
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
2023-10-10 16:26:46 +02:00
* Remove this unnecessary 'using'.
'''
== Comments And Links
(visible only on this page)
2023-10-10 16:26:46 +02:00
include::../comments-and-links.adoc[]
=== on 23 Sep 2019, 16:37:14 Nicolas Harraudeau wrote:
*OUT OF SCOPE*
Duplicate imports are out of scopes as Roslyn already raises an issue in this case. As Roslyn issues are enabled by default this would simply create duplicates.
=== on 18 Dec 2020, 10:06:15 Andrei Epure wrote:
We are removing this rule from SonarWay due to its performance issues. After the rule gets re-designed to avoid perf issues, (see https://github.com/SonarSource/sonar-dotnet/issues/3761[#3761]), we should bring it back to SonarWay.
endif::env-github,rspecator-view[]