Skip to content

Commit 9500888

Browse files
sebastien-marichalsonartech
authored andcommitted
NET-1361 Fix S3444 AD0001: Secondary location in a bigger file
1 parent abce8b8 commit 9500888

File tree

4 files changed

+125
-22
lines changed

4 files changed

+125
-22
lines changed

analyzers/src/SonarAnalyzer.CSharp/Rules/InheritedCollidingInterfaceMembers.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ private static IEnumerable<SecondaryLocation> SecondaryLocations(List<CollidingM
6969
.Where(x => x.Location.IsInSource)
7070
.Select(x => x.Location.ToSecondary(x.CollideWithSymbolName is { Length: > 0 } ? SecondaryMessageFormat : null, x.CollideWithSymbolName));
7171

72-
string CollideWithSymbolName(CollidingMember collidingMember) =>
73-
collidingMember.CollideWith.Locations.FirstOrDefault() is { } location
74-
? GetMemberDisplayName(collidingMember.CollideWith, location.SourceSpan.Start, model, [SymbolDisplayPartKind.ClassName, SymbolDisplayPartKind.InterfaceName])
72+
string CollideWithSymbolName(CollidingMember collidingMember)
73+
{
74+
return collidingMember.CollideWith.Locations.FirstOrDefault() is { } location
75+
&& location.SourceTree.SemanticModelOrDefault(model) is { } semanticModel
76+
? GetMemberDisplayName(collidingMember.CollideWith, location.SourceSpan.Start, semanticModel, [SymbolDisplayPartKind.ClassName, SymbolDisplayPartKind.InterfaceName])
7577
: string.Empty;
78+
}
7679
}
7780

7881
private static IEnumerable<CollidingMember> GetCollidingMembers(ITypeSymbol interfaceSymbol)

analyzers/tests/SonarAnalyzer.Test/Rules/InheritedCollidingInterfaceMembersTest.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,37 @@
1616

1717
using SonarAnalyzer.CSharp.Rules;
1818

19-
namespace SonarAnalyzer.Test.Rules
20-
{
21-
[TestClass]
22-
public class InheritedCollidingInterfaceMembersTest
23-
{
24-
private readonly VerifierBuilder builder = new VerifierBuilder<InheritedCollidingInterfaceMembers>();
25-
26-
[TestMethod]
27-
public void InheritedCollidingInterfaceMembers() =>
28-
builder.AddPaths("InheritedCollidingInterfaceMembers.cs").Verify();
19+
namespace SonarAnalyzer.Test.Rules;
2920

30-
[TestMethod]
31-
public void InheritedCollidingInterfaceMembers_CSharp8() =>
32-
builder.AddPaths("InheritedCollidingInterfaceMembers.CSharp8.cs")
33-
.WithOptions(LanguageOptions.FromCSharp8)
34-
.AddReferences(MetadataReferenceFacade.NetStandard21)
35-
.Verify();
21+
[TestClass]
22+
public class InheritedCollidingInterfaceMembersTest
23+
{
24+
private readonly VerifierBuilder builder = new VerifierBuilder<InheritedCollidingInterfaceMembers>();
25+
26+
[TestMethod]
27+
public void InheritedCollidingInterfaceMembers() =>
28+
builder.AddPaths("InheritedCollidingInterfaceMembers.cs").Verify();
29+
30+
[TestMethod]
31+
public void InheritedCollidingInterfaceMembers_DifferentFileSizes() =>
32+
builder.AddPaths(
33+
"InheritedCollidingInterfaceMembers.DifferentFile.cs",
34+
"InheritedCollidingInterfaceMembers.AnotherFile.cs")
35+
.Verify();
36+
37+
[TestMethod]
38+
public void InheritedCollidingInterfaceMembers_CSharp8() =>
39+
builder.AddPaths("InheritedCollidingInterfaceMembers.CSharp8.cs")
40+
.WithOptions(LanguageOptions.FromCSharp8)
41+
.AddReferences(MetadataReferenceFacade.NetStandard21)
42+
.Verify();
3643

3744
#if NET
38-
3945
[TestMethod]
4046
public void InheritedCollidingInterfaceMembers_CSharp11() =>
4147
builder.AddPaths("InheritedCollidingInterfaceMembers.CSharp11.cs")
4248
.WithOptions(LanguageOptions.FromCSharp11)
4349
.Verify();
4450

4551
#endif
46-
47-
}
4852
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* We want to create a big padding so when the rule looking for the secondary location, we make sure
3+
* that this file is way bigger that the file were the primary location is.
4+
* So the secondray location would be out of bound of the first file.
5+
*
6+
* ###################################################################################
7+
* ### .i;;;;i. ###
8+
* ### iYcviii;vXY: ###
9+
* ### .YXi .i1c. ###
10+
* ### .YC. . in7. ###
11+
* ### .vc. ...... ;1c. ###
12+
* ### i7, .. .;1; ###
13+
* ### i7, .. ... .Y1i ###
14+
* ### ,7v .6MMM@; .YX, ###
15+
* ### .7;. ..IMMMMMM1 :t7. ###
16+
* ### .;Y. ;$MMMMMM9. :tc. ###
17+
* ### vY. .. .nMMM@MMU. ;1v. ###
18+
* ### i7i ... .#MM@M@C. .....:71i ###
19+
* ### it: .... $MMM@9;.,i;;;i,;tti ###
20+
* ### :t7. ..... 0MMMWv.,iii:::,,;St. ###
21+
* ### .nC. ..... IMMMQ..,::::::,.,czX. ###
22+
* ### .ct: ....... .ZMMMI..,:::::::,,:76Y. ###
23+
* ### c2: ......,i..Y$M@t..:::::::,,..inZY ###
24+
* ### vov ......:ii..c$MBc..,,,,,,,,,,..iI9i ###
25+
* ### i9Y ......iii:..7@MA,..,,,,,,,,,....;AA: ###
26+
* ### iIS. ......:ii::..;@MI....,............;Ez. ###
27+
* ### .I9. ......:i::::...8M1..................C0z. ###
28+
* ### .z9; ......:i::::,.. .i:...................zWX. ###
29+
* ### vbv ......,i::::,,. ................. :AQY ###
30+
* ### c6Y. .,...,::::,,..:t0@@QY. ................ :8bi ###
31+
* ### :6S. ..,,...,:::,,,..EMMMMMMI. ............... .;bZ, ###
32+
* ### :6o, .,,,,..:::,,,..i#MMMMMM#v................. YW2. ###
33+
* ### .n8i ..,,,,,,,::,,,,.. tMMMMM@C:.................. .1Wn ###
34+
* ### 7Uc. .:::,,,,,::,,,,.. i1t;,..................... .UEi ###
35+
* ### 7C...::::::::::::,,,,.. .................... vSi. ###
36+
* ### ;1;...,,::::::,......... .................. Yz: ###
37+
* ### v97,......... .voC. ###
38+
* ### izAotX7777777777777777777777777777777777777777Y7n92: ###
39+
* ### .;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov. ###
40+
* ### ###
41+
* ### ###
42+
* ### This file MUST be bigger than ###
43+
* ### InheritedCollidingInterfaceMembers.DifferentFile.cs in size ###
44+
* ### ###
45+
* ###################################################################################
46+
*
47+
* A lorem ipsum text
48+
*
49+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor
50+
* incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
51+
* exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
52+
* dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
53+
* Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
54+
* anim id est laborum.
55+
*
56+
* Another one:
57+
*
58+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor
59+
* incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
60+
* exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
61+
* dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
62+
* Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
63+
* anim id est laborum.
64+
*
65+
*/
66+
67+
using System;
68+
using System.Collections;
69+
using System.Collections.Generic;
70+
71+
namespace Tests.Diagnostics
72+
{
73+
public partial interface IAnotherFile
74+
{
75+
void Method(int i);
76+
}
77+
78+
public partial interface IAnotherFile2
79+
{
80+
void Method(int i); // Secondary {{This member collides with 'IAnotherFile.Method(int)'}}
81+
}
82+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
/*
6+
* DO NOT ADD ANYTHING ELSE HERE
7+
*/
8+
9+
namespace Tests.Diagnostics
10+
{
11+
public interface IParentInAnotherFile : IAnotherFile, IAnotherFile2 // Noncompliant {{Rename or add member 'Method(int)' to this interface to resolve ambiguities.}}
12+
{
13+
}
14+
}

0 commit comments

Comments
 (0)