diff --git a/src/Meziantou.Analyzer/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzer.cs b/src/Meziantou.Analyzer/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzer.cs index 5b55340c..c825703c 100644 --- a/src/Meziantou.Analyzer/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzer.cs @@ -146,6 +146,9 @@ public void AnalyzeSymbol(SymbolAnalysisContext context) break; case IMethodSymbol methodSymbol when methodSymbol.MethodKind is not MethodKind.PropertyGet and not MethodKind.PropertySet: + if (context.Symbol is IMethodSymbol { MethodKind: MethodKind.Conversion }) + return; + if (!IsValidType(methodSymbol.ReturnType)) { context.ReportDiagnostic(Rule, methodSymbol, DiagnosticMethodReportOptions.ReportOnReturnType); diff --git a/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs b/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs index 96686d6c..6210a9d8 100755 --- a/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs +++ b/tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.Validation.cs @@ -1,13 +1,5 @@ -using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; using Meziantou.Analyzer.Test.Helpers; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; @@ -17,7 +9,6 @@ using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Simplification; using Microsoft.CodeAnalysis.Text; -using Xunit; namespace TestHelper; diff --git a/tests/Meziantou.Analyzer.Test/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzerTests.cs index cb6a6226..2f9eff6a 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/PreferReturningCollectionAbstractionInsteadOfImplementationAnalyzerTests.cs @@ -371,4 +371,44 @@ public class Test : ITest """) .ValidateAsync(); } + + [Fact] + public async Task ConversionOperator() + { + await CreateProjectBuilder() + .WithSourceCode(""" + public class Sample + { + public static implicit operator Sample(System.Collections.Generic.List _) => throw null; + public static implicit operator System.Collections.Generic.List(Sample _) => throw null; + } + """) + .ValidateAsync(); + } + + [Fact] + public async Task AddOperator() + { + await CreateProjectBuilder() + .WithSourceCode(""" + public class Sample + { + public static Sample operator+(Sample instance, [|System.Collections.Generic.List|] value) => throw null; + } + """) + .ValidateAsync(); + } + + [Fact] + public async Task AddOperator_Instance() + { + await CreateProjectBuilder() + .WithSourceCode(""" + public class Sample : System.Collections.Generic.List + { + public static Sample operator+(Sample instance, int value) => throw null; + } + """) + .ValidateAsync(); + } }