diff --git a/src/Meziantou.Analyzer/Rules/DoNotLogClassifiedDataAnalyzer.cs b/src/Meziantou.Analyzer/Rules/DoNotLogClassifiedDataAnalyzer.cs index 637bb301..bcd5c35d 100644 --- a/src/Meziantou.Analyzer/Rules/DoNotLogClassifiedDataAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/DoNotLogClassifiedDataAnalyzer.cs @@ -98,23 +98,23 @@ private void ValidateDataClassification(DiagnosticReporter diagnosticReporter, I static void ValidateDataClassification(DiagnosticReporter diagnosticReporter, IOperation operation, IOperation reportOperation, INamedTypeSymbol dataClassificationAttributeSymbol) { operation = operation.UnwrapConversionOperations(); - if (operation is IParameterReferenceOperation parameterReferenceOperation) + if (operation is IParameterReferenceOperation { Parameter: var parameter }) { - if (parameterReferenceOperation.Parameter.HasAttribute(dataClassificationAttributeSymbol, inherits: true)) + if (parameter.HasAttribute(dataClassificationAttributeSymbol, inherits: true) || parameter.Type.HasAttribute(dataClassificationAttributeSymbol, inherits: true)) { diagnosticReporter.ReportDiagnostic(Rule, reportOperation); } } - else if (operation is IPropertyReferenceOperation propertyReferenceOperation) + else if (operation is IPropertyReferenceOperation { Property: var property }) { - if (propertyReferenceOperation.Property.HasAttribute(dataClassificationAttributeSymbol, inherits: true)) + if (property.HasAttribute(dataClassificationAttributeSymbol, inherits: true) || property.ContainingType.HasAttribute(dataClassificationAttributeSymbol, inherits: true)) { diagnosticReporter.ReportDiagnostic(Rule, reportOperation); } } - else if (operation is IFieldReferenceOperation fieldReferenceOperation) + else if (operation is IFieldReferenceOperation { Field: var field }) { - if (fieldReferenceOperation.Field.HasAttribute(dataClassificationAttributeSymbol, inherits: true)) + if (field.HasAttribute(dataClassificationAttributeSymbol, inherits: true) || field.ContainingType.HasAttribute(dataClassificationAttributeSymbol, inherits: true)) { diagnosticReporter.ReportDiagnostic(Rule, reportOperation); } diff --git a/tests/Meziantou.Analyzer.Test/Rules/DoNotLogClassifiedDataAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/DoNotLogClassifiedDataAnalyzerTests.cs index 48c70401..7b54fca2 100755 --- a/tests/Meziantou.Analyzer.Test/Rules/DoNotLogClassifiedDataAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/DoNotLogClassifiedDataAnalyzerTests.cs @@ -143,7 +143,36 @@ await CreateProjectBuilder() } [Fact] - public async Task Logger_BeginScope_DataClassification_Parameter() + public async Task Logger_LogInformation_DataClassification_Parameter_AttributeOnType() + { + const string SourceCode = """ +using Microsoft.Extensions.Logging; + +ILogger logger = null; + +void A([TaxonomyAttribute]int param) +{ + logger.LogInformation("{Prop}", [|param|]); +} + +[TaxonomyAttribute()] +class Dummy +{ + public string Prop; +} + +class TaxonomyAttribute : Microsoft.Extensions.Compliance.Classification.DataClassificationAttribute +{ + public TaxonomyAttribute() : base(Microsoft.Extensions.Compliance.Classification.DataClassification.Unknown) { } +} +"""; + await CreateProjectBuilder() + .WithSourceCode(SourceCode) + .ValidateAsync(); + } + + [Fact] + public async Task Logger_BeginScope_DataClassification_Property() { const string SourceCode = """ using Microsoft.Extensions.Logging;