diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs index 92fc9074..f3919f78 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs @@ -38,6 +38,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests public NetInt type { get; } = new NetInt { Value = 42 }; public NetRef refField { get; } = null; } + class SObject : Item { } } namespace SampleMod @@ -47,9 +48,10 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests public void Entry() { Item item = null; + SObject obj = null; // this line should raise diagnostics - {{test-code}} // line 36 + {{test-code}} // line 38 // these lines should not if (item.type.Value != 42); @@ -59,7 +61,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests "; /// The line number where the unit tested code is injected into . - private const int SampleCodeLine = 36; + private const int SampleCodeLine = 38; /// The column number where the unit tested code is injected into . private const int SampleCodeColumn = 25; @@ -93,6 +95,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests [TestCase("if (item.type != 42);", 4, "item.type", "NetInt", "int")] [TestCase("if (item.refField != null);", 4, "item.refField", "NetRef", "object")] [TestCase("if (item?.type != 42);", 4, "item?.type", "NetInt", "int")] + [TestCase("if (obj.type != 42);", 4, "obj.type", "NetInt", "int")] public void AvoidImplicitNetFieldComparisons_RaisesDiagnostic(string codeText, int column, string expression, string fromType, string toType) { // arrange @@ -118,6 +121,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests [TestCase("int category = item.category;", 15, "item.category", "NetInt", "Category")] [TestCase("int category = (item).category;", 15, "(item).category", "NetInt", "Category")] [TestCase("int category = ((Item)item).category;", 15, "((Item)item).category", "NetInt", "Category")] + [TestCase("int category = obj.category;", 15, "obj.category", "NetInt", "Category")] public void AvoidNetFields_RaisesDiagnostic(string codeText, int column, string expression, string netType, string suggestedProperty) { // arrange diff --git a/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs b/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs index a9987733..2cb1ac4c 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs @@ -209,9 +209,13 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer string propertyName = node.Name.Identifier.Text; // suggest replacement - if (this.NetFieldWrapperProperties.TryGetValue($"{declaringType}::{propertyName}", out string suggestedPropertyName)) + for (ITypeSymbol type = declaringType; type != null; type = type.BaseType) { - context.ReportDiagnostic(Diagnostic.Create(this.Rules["SMAPI002"], context.Node.GetLocation(), node, memberType.Type.Name, suggestedPropertyName)); + if (this.NetFieldWrapperProperties.TryGetValue($"{type}::{propertyName}", out string suggestedPropertyName)) + { + context.ReportDiagnostic(Diagnostic.Create(this.Rules["SMAPI002"], context.Node.GetLocation(), node, memberType.Type.Name, suggestedPropertyName)); + break; + } } } catch (Exception ex)