From 8d47d71144cf6803cde3e2a78c48b15d90490e64 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Sat, 23 Nov 2024 10:33:40 +0100 Subject: [PATCH] Fix Unity method detection (#1586) --- ChangeLog.md | 4 ++++ .../UnusedMember/UnusedMemberAnalyzer.cs | 4 +++- ...S1213RemoveUnusedMemberDeclarationTests.cs | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 969cbff900..bcf23fb26d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fix analyzer [RCS1213](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1213) ([PR](https://github.com/dotnet/roslynator/pull/1586)) + ## [4.12.9] - 2024-10-25 ### Fixed diff --git a/src/Analyzers/CSharp/Analysis/UnusedMember/UnusedMemberAnalyzer.cs b/src/Analyzers/CSharp/Analysis/UnusedMember/UnusedMemberAnalyzer.cs index f9f88ca165..6bdc0e5239 100644 --- a/src/Analyzers/CSharp/Analysis/UnusedMember/UnusedMemberAnalyzer.cs +++ b/src/Analyzers/CSharp/Analysis/UnusedMember/UnusedMemberAnalyzer.cs @@ -155,7 +155,9 @@ private static void AnalyzeTypeDeclaration(SyntaxNodeAnalysisContext context) if (IsMainMethod(declaration, modifiers, methodName)) break; - if (declaration.ReturnsVoid() + if ((declaration.ReturnsVoid() + || (methodName == "Start" + && semanticModel.GetDeclaredSymbol(declaration, cancellationToken)?.ReturnType.SpecialType == SpecialType.System_Collections_IEnumerator)) && context.IsUnityCodeAnalysisEnabled() == true) { if (canContainUnityScriptMethods is null) diff --git a/src/Tests/Analyzers.Tests/RCS1213RemoveUnusedMemberDeclarationTests.cs b/src/Tests/Analyzers.Tests/RCS1213RemoveUnusedMemberDeclarationTests.cs index b2b05d2f65..e5d94a74a7 100644 --- a/src/Tests/Analyzers.Tests/RCS1213RemoveUnusedMemberDeclarationTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1213RemoveUnusedMemberDeclarationTests.cs @@ -380,6 +380,30 @@ private void Awake() } } +namespace UnityEngine +{ + class MonoBehaviour + { + } +} +", options: Options.AddConfigOption(ConfigOptionKeys.UnityCodeAnalysisEnabled, true)); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveUnusedMemberDeclaration)] + public async Task TestNoDiagnostic_UnityScriptMethods_Start() + { + await VerifyNoDiagnosticAsync(@" +using System.Collections; +using UnityEngine; + +class C : MonoBehaviour +{ + private IEnumerator Start() + { + yield break; + } +} + namespace UnityEngine { class MonoBehaviour