Skip to content

Commit 45fe392

Browse files
authored
[generator] BG8403 when type name matches enclosing namespace name (#1010)
Fixes: #982 As elaborated upon in [Eric Lippert's blog][0] series "Do not name a class the same as its namespace" ([Part 1][1], [Part 2][2], [Part 3][3], and [Part 4][4]), and as explained in the [Framework Design Guidelines][5]: > ❌ DO NOT use the same name for a namespace and a type in that namespace. > > For example, do not use `Debug` as a namespace name and then also > provide a class named `Debug` in the same namespace. > Several compilers require such types to be fully qualified. emit a BG8403 warning when a type is generated which matches the enclosing namespace "part": namespace Android.Graphics.Drawable { partial class Drawable { } } Such constructs will now emit the warning: warning BG8403: Type `Android.Graphics.Drawable.Drawable` has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information. This construct not a warning in Roslyn, and bindings code is autogenerated, so it can be hard for bindings authors to notice that they are making this mistake. <https://aka.ms/BG8403> explains the issue and possible solutions, which generally includes using [`@(AndroidNamespaceReplacement)`][6] to rename the namespace (e.g. to `Android.Graphics.Drawables`) or using `Metadata.xml` to rename the type. ~~ Testing Note ~~ It is very hard to unit test `generator` warning/error reporting, since the `Report` type is a global static class. Add a static property `Report.OutputDelegate` that allows the tester to temporarily set a different output method that tests can use to verify `Report` output. These tests should be marked `[NonParallelizable]` since they affect the global state shared with any other running tests. [0]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert [1]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-one [2]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-two [3]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-three [4]: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/do-not-name-a-class-the-same-as-its-namespace-part-four [5]: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces [6]: dotnet/android@398afd4
1 parent fe60483 commit 45fe392

File tree

20 files changed

+203
-4
lines changed

20 files changed

+203
-4
lines changed

src/Java.Interop.Localization/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Java.Interop.Localization/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ In this message, the term "constants" refers to class or interface members that
201201
{1} - .NET field name.
202202
{2} - Java type.</comment>
203203
</data>
204+
<data name="Generator_BG8403" xml:space="preserve">
205+
<value>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</value>
206+
<comment>{0} - Java type.</comment>
207+
</data>
204208
<data name="Generator_BG8500" xml:space="preserve">
205209
<value>Unexpected child element of '&lt;interface&gt;': '{0}'.</value>
206210
<comment>{0} - XML element name.

src/Java.Interop.Localization/xlf/Resources.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.ko.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.pl.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.pt-BR.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.ru.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.tr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.zh-Hans.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Localization/xlf/Resources.zh-Hant.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ In this message, the term "constants" refers to class or interface members that
104104
{1} - .NET field name.
105105
{2} - Java type.</note>
106106
</trans-unit>
107+
<trans-unit id="Generator_BG8403">
108+
<source>Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</source>
109+
<target state="new">Type '{0}' has a type name which matches the enclosing namespace name. See https://aka.ms/BG8403 for more information.</target>
110+
<note>{0} - Java type.</note>
111+
</trans-unit>
107112
<trans-unit id="Generator_BG8500">
108113
<source>Unexpected child element of '&lt;interface&gt;': '{0}'.</source>
109114
<target state="new">Unexpected child element of '&lt;interface&gt;': '{0}'.</target>

src/Java.Interop.Tools.Generator/Utilities/Report.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Xml;
34
using System.Xml.Linq;
45

@@ -7,6 +8,7 @@ namespace Java.Interop.Tools.Generator
78
public class Report
89
{
910
public static int? Verbosity { get; set; }
11+
public static Action<TraceLevel, string>? OutputDelegate { get; set; }
1012

1113
public class LocalizedMessage
1214
{
@@ -43,6 +45,7 @@ public LocalizedMessage (int code, string value)
4345
public static LocalizedMessage WarningFieldNameCollision_Method => new LocalizedMessage (0x8401, Localization.Resources.Generator_BG8401_Method);
4446
public static LocalizedMessage WarningFieldNameCollision_NestedType => new LocalizedMessage (0x8401, Localization.Resources.Generator_BG8401_NestedType);
4547
public static LocalizedMessage WarningDuplicateField => new LocalizedMessage (0x8402, Localization.Resources.Generator_BG8402);
48+
public static LocalizedMessage WarningTypeNameMatchesEnclosingNamespace => new LocalizedMessage (0x8403, Localization.Resources.Generator_BG8403);
4649
public static LocalizedMessage WarningUnexpectedInterfaceChild => new LocalizedMessage (0x8500, Localization.Resources.Generator_BG8500);
4750
public static LocalizedMessage WarningEmptyEventName => new LocalizedMessage (0x8501, Localization.Resources.Generator_BG8501);
4851
public static LocalizedMessage WarningInvalidDueToInterfaces => new LocalizedMessage (0x8502, Localization.Resources.Generator_BG8502);
@@ -95,7 +98,7 @@ public static void LogCodedError (LocalizedMessage message, XNode? node, params
9598

9699
public static void LogCodedError (LocalizedMessage message, string? sourceFile, int line, int column, params string? [] args)
97100
{
98-
Console.Error.WriteLine (Format (true, message.Code, sourceFile, line, column, message.Value, args));
101+
WriteOutput (TraceLevel.Error, Format (true, message.Code, sourceFile, line, column, message.Value, args));
99102
}
100103

101104
public static void LogCodedWarning (int verbosity, LocalizedMessage message, params string? [] args)
@@ -120,17 +123,17 @@ public static void LogCodedWarning (int verbosity, LocalizedMessage message, Exc
120123
return;
121124

122125
var supp = innerException != null ? " For details, see verbose output." : null;
123-
Console.Error.WriteLine (Format (false, message.Code, sourceFile, line, column, message.Value, args) + supp);
126+
WriteOutput (TraceLevel.Warning, Format (false, message.Code, sourceFile, line, column, message.Value, args) + supp);
124127

125128
if (innerException != null)
126-
Console.Error.WriteLine (innerException);
129+
WriteOutput (TraceLevel.Warning, innerException.ToString ());
127130
}
128131

129132
public static void Verbose (int verbosity, string format, params object?[] args)
130133
{
131134
if (verbosity > (Verbosity ?? 0))
132135
return;
133-
Console.Error.WriteLine (format, args);
136+
WriteOutput (TraceLevel.Verbose, format, args);
134137
}
135138

136139
public static string FormatCodedMessage (bool error, LocalizedMessage message, params object? [] args)
@@ -169,6 +172,18 @@ public static string Format (bool error, int errorCode, string? sourceFile, int
169172

170173
return (file, pos?.LineNumber ?? -1, pos?.LinePosition ?? -1);
171174
}
175+
176+
static void WriteOutput (TraceLevel traceLevel, string format, params object?[] args)
177+
{
178+
// Write to overridden output if requested
179+
if (OutputDelegate != null) {
180+
OutputDelegate (traceLevel, string.Format (format, args));
181+
return;
182+
}
183+
184+
// Write to Console.Error
185+
Console.Error.WriteLine (format, args);
186+
}
172187
}
173188

174189
/// <summary>

tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
5+
using System.Text;
46
using generator.SourceWriters;
7+
using Java.Interop.Tools.Generator;
58
using MonoDroid.Generation;
69
using NUnit.Framework;
710
using Xamarin.Android.Binder;
@@ -527,6 +530,47 @@ public void ObsoleteBoundMethodAbstractDeclaration ()
527530
// Ensure [Obsolete] was written
528531
Assert.True (writer.ToString ().Contains ("[Obsolete (@\"This is so old!\")]"), writer.ToString ());
529532
}
533+
534+
[Test]
535+
[NonParallelizable] // We are setting a static property on Report
536+
public void WarnIfTypeNameMatchesNamespace ()
537+
{
538+
var @class = new TestClass ("Object", "java.myclass.MyClass");
539+
var sb = new StringBuilder ();
540+
541+
var write_output = new Action<TraceLevel, string> ((t, s) => { sb.AppendLine (s); });
542+
Report.OutputDelegate = write_output;
543+
544+
generator.Context.ContextTypes.Push (@class);
545+
generator.WriteType (@class, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
546+
generator.Context.ContextTypes.Pop ();
547+
548+
Report.OutputDelegate = null;
549+
550+
// Ensure the warning was raised
551+
Assert.True (sb.ToString ().Contains ("warning BG8403"));
552+
}
553+
554+
[Test]
555+
[NonParallelizable] // We are setting a static property on Report
556+
public void DontWarnIfNestedTypeNameMatchesNamespace ()
557+
{
558+
var @class = new TestClass ("Object", "java.myclass.MyParentClass");
559+
@class.NestedTypes.Add (new TestClass ("Object", "java.myclass.MyParentClass.MyClass"));
560+
var sb = new StringBuilder ();
561+
562+
var write_output = new Action<TraceLevel, string> ((t, s) => { sb.AppendLine (s); });
563+
Report.OutputDelegate = write_output;
564+
565+
generator.Context.ContextTypes.Push (@class);
566+
generator.WriteType (@class, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
567+
generator.Context.ContextTypes.Pop ();
568+
569+
Report.OutputDelegate = null;
570+
571+
// The warning should not be raised if the nested type matches enclosing namespace
572+
Assert.False (sb.ToString ().Contains ("warning BG8403"));
573+
}
530574
}
531575

532576
[TestFixture]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace generator
5+
{
6+
static class StringExtensions
7+
{
8+
/// <summary>
9+
/// Shortcut for !string.IsNullOrWhiteSpace (s)
10+
/// </summary>
11+
public static bool HasValue (this string s) => !string.IsNullOrWhiteSpace (s);
12+
13+
/// <summary>
14+
/// Removes the final subset of a delimited string. ("127.0.0.1" -> "127.0.0")
15+
/// </summary>
16+
public static string ChompLast (this string s, char separator)
17+
{
18+
if (!s.HasValue ())
19+
return s;
20+
21+
var index = s.LastIndexOf (separator);
22+
23+
if (index < 0)
24+
return string.Empty;
25+
26+
return s.Substring (0, index);
27+
}
28+
29+
/// <summary>
30+
/// Returns the final subset of a delimited string. ("127.0.0.1" -> "1")
31+
/// </summary>
32+
public static string LastSubset (this string s, char separator)
33+
{
34+
if (!s.HasValue ())
35+
return s;
36+
37+
var index = s.LastIndexOf (separator);
38+
39+
if (index < 0)
40+
return s;
41+
42+
return s.Substring (index + 1);
43+
}
44+
}
45+
}

tools/generator/Java.Interop.Tools.Generator.CodeGeneration/JavaInteropCodeGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public override void WriteType (GenBase gen, string indent, GenerationInfo gen_i
4444
else
4545
throw new InvalidOperationException ("Unknown GenBase type");
4646

47+
// We do this here because we only want to check for top-level types,
48+
// we should not check types nested in other types.
49+
SourceWriterExtensions.WarnIfTypeNameMatchesNamespace (type_writer, gen);
50+
4751
var cw = new CodeWriter (writer, indent);
4852
type_writer.Write (cw);
4953
}

tools/generator/SourceWriters/Extensions/SourceWriterExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,5 +397,18 @@ public static TypeWriter BuildManagedTypeModel (GenBase gen, CodeGenerationOptio
397397

398398
throw new InvalidOperationException ("Unknown GenBase type");
399399
}
400+
401+
public static void WarnIfTypeNameMatchesNamespace (TypeWriter type, GenBase gen)
402+
{
403+
// We only care about the last part of a namespace
404+
// eg: android.graphics.drawable -> drawable
405+
var ns = gen.Namespace?.LastSubset ('.');
406+
407+
if (!ns.HasValue ())
408+
return;
409+
410+
if (string.Equals (ns, type.Name, StringComparison.OrdinalIgnoreCase))
411+
Report.LogCodedWarning (0, Report.WarningTypeNameMatchesEnclosingNamespace, $"{gen.Namespace}.{type.Name}");
412+
}
400413
}
401414
}

0 commit comments

Comments
 (0)