sonar-dotnet
sonar-dotnet copied to clipboard
Fix CodeFix S3217: Using directives are not moved to the closest namespace when this is file scoped.
Description
S3261 code fix should move the imports to the closest namespace. This does not happen when the namespace is file scoped.
Repro steps
Before codefix
global using System.Collections;
global using System.Collections.Generic;
namespace Tests.Diagnostics;
using System;
class A : I { }
class B : A { }
record Record
{
public void M2(IEnumerable<A> enumerable)
{
foreach (A i in enumerable)
{ }
foreach (B i in enumerable) // Noncompliant
{ }
}
}
After codefix:
global using System.Collections;
global using System.Collections.Generic;
using System.Linq; // this should not be placed here
namespace Tests.Diagnostics;
using System;
// the System.Linq using should be here as this is the closest namespace
interface I { }
class A : I { }
class B : A { }
record Record
{
public void M2(IEnumerable<A> enumerable)
{
foreach (A i in enumerable)
{ }
foreach (B i in enumerable.OfType<B>()) // Fixed
{ }
}
}
Related information
- C#/VB.NET Plugins version: 8.43.0.51858