Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle OnSerializedAttribute (RCS1163) #1280

Merged
merged 6 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix analyzer [RCS1124](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1124) ([PR](https://github.com/dotnet/roslynator/pull/1279))
- Fix analyzer [RCS0058](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0058) ([PR](https://github.com/dotnet/roslynator/pull/1281))
- Fix analyzer [RCS1163](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1163) ([PR](https://github.com/dotnet/roslynator/pull/1280))

## [4.6.4] - 2023-11-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -14,6 +15,14 @@ namespace Roslynator.CSharp.Analysis.UnusedParameter;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class UnusedParameterAnalyzer : BaseDiagnosticAnalyzer
{
private static readonly MetadataNameSet _attributes = new(new[]
{
MetadataName.Parse("System.Runtime.Serialization.OnSerializedAttribute"),
MetadataName.Parse("System.Runtime.Serialization.OnDeserializedAttribute"),
MetadataName.Parse("System.Runtime.Serialization.OnSerializingAttribute"),
MetadataName.Parse("System.Runtime.Serialization.OnDeserializingAttribute"),
});

private static ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics;

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
Expand Down Expand Up @@ -134,7 +143,7 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)

walker.SetValues(context.SemanticModel, context.CancellationToken);

FindUnusedNodes(parameterInfo, walker);
FindUnusedNodes(parameterInfo, walker, methodSymbol);

if (walker.Nodes.Count > 0
&& !MethodReferencedAsMethodGroupWalker.IsReferencedAsMethodGroup(methodDeclaration, methodSymbol, context.SemanticModel, context.CancellationToken))
Expand Down Expand Up @@ -331,19 +340,21 @@ private static void Analyze(SyntaxNodeAnalysisContext context, in ParameterInfo
}
}

private static void FindUnusedNodes(in ParameterInfo parameterInfo, UnusedParameterWalker walker)
private static void FindUnusedNodes(in ParameterInfo parameterInfo, UnusedParameterWalker walker, IMethodSymbol methodSymbol = null)
{
if (parameterInfo.Parameter is not null
&& !IsArgListOrDiscard(parameterInfo.Parameter))
{
walker.AddParameter(parameterInfo.Parameter);
AddParameter(walker, parameterInfo.Parameter, 0, methodSymbol);
}
else
{
foreach (ParameterSyntax parameter in parameterInfo.Parameters)
for (int i = 0; i < parameterInfo.Parameters.Count; i++)
{
ParameterSyntax parameter = parameterInfo.Parameters[i];

if (!IsArgListOrDiscard(parameter))
walker.AddParameter(parameter);
AddParameter(walker, parameter, i, methodSymbol);
}
}

Expand All @@ -357,6 +368,26 @@ private static void FindUnusedNodes(in ParameterInfo parameterInfo, UnusedParame
walker.Visit(parameterInfo.Node);
}

private static void AddParameter(UnusedParameterWalker walker, ParameterSyntax parameter, int index, IMethodSymbol methodSymbol)
{
if (methodSymbol is not null)
{
if (index >= methodSymbol.Parameters.Length)
{
Debug.Fail($"{index} {methodSymbol.Parameters.Length}");
return;
}

if (methodSymbol.Parameters[index].Type.HasMetadataName(MetadataNames.System_Runtime_Serialization_StreamingContext)
&& _attributes.ContainsAny(methodSymbol.GetAttributes()))
{
return;
}
}

walker.AddParameter(parameter);
}

private static bool IsArgListOrDiscard(ParameterSyntax parameter)
{
return parameter.Identifier.IsKind(SyntaxKind.ArgListKeyword)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ public bool Contains(ISymbol symbol)

return false;
}

public bool ContainsAny(ImmutableArray<AttributeData> attributes)
{
foreach (AttributeData attributeData in attributes)
{
if (attributeData.AttributeClass is not null
&& Contains(attributeData.AttributeClass))
{
return true;
}
}

return false;
}
}
35 changes: 35 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1163UnusedParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,41 @@ namespace System.Windows
public class DependencyPropertyChangedEventArgs
{
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnusedParameter)]
public async Task TestNoDiagnostic_StreamingContextAttributes()
{
await VerifyNoDiagnosticAsync(@"
using System;
using System.Runtime.Serialization;

class C
{
[Obsolete]
[OnSerialized]
void M1(string p, StreamingContext context)
{
var x = p;
}

[OnDeserialized]
void M2(StreamingContext context, string p)
{
var x = p;
}

[OnSerializing]
void M3(StreamingContext context)
{
}

[OnDeserializing]
void M4(StreamingContext context)
{
}
}
");
}
Expand Down