/
ReferingToObjectAndReassigningItInTheSameStatement.cs
96 lines (82 loc) · 4.47 KB
/
ReferingToObjectAndReassigningItInTheSameStatement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines
{
/// <summary>
/// ReferenceChanged: Prevent objects from being referenced in statements where they are reassigned
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class ReferingToObjectAndReassigningItInTheSameStatement : DiagnosticAnalyzer
{
internal const string RuleId = "ReferenceChanged";
private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferingToObjectAndReassigningItInTheSameStatementTitle), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources));
private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferingToObjectAndReassigningItInTheSameStatementMessage), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources));
internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId,
s_localizableTitle,
s_localizableMessage,
DiagnosticCategory.Usage,
DiagnosticHelpers.DefaultDiagnosticSeverity,
isEnabledByDefault: DiagnosticHelpers.EnabledByDefaultIfNotBuildingVSIX,
helpLinkUri: null);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(AnalyzeAssignment, OperationKind.SimpleAssignment);
}
private void AnalyzeAssignment(OperationAnalysisContext context)
{
var assignmentOperation = (ISimpleAssignmentOperation)context.Operation;
// Check if there are more then one assignment in a statement
if (!(assignmentOperation.Target is IMemberReferenceOperation operationTarget))
{
return;
}
// This analyzer makes sense only for reference type objects
if (operationTarget.Instance?.Type.IsValueType == true)
{
return;
}
// Search for object equal to operationTarget.Instance further in assignment chain
bool isViolationFound = false;
string violatingObjectName;
if (operationTarget.Instance is ILocalReferenceOperation localInstance)
{
violatingObjectName = localInstance.Local.Name;
isViolationFound = AnalyzeMemberAssignment(assignmentOperation, localInstance, (a, b) => a.Local == b.Local);
}
else if (operationTarget.Instance is IMemberReferenceOperation memberInstance)
{
violatingObjectName = memberInstance.Member.Name;
isViolationFound = AnalyzeMemberAssignment(assignmentOperation, memberInstance, (a, b) => a.Member == b.Member);
}
else
{
return;
}
if (isViolationFound)
{
var diagnostic = Diagnostic.Create(Rule, operationTarget.Syntax.GetLocation(), violatingObjectName);
context.ReportDiagnostic(diagnostic);
}
}
private static bool AnalyzeMemberAssignment<T>(ISimpleAssignmentOperation assignmentOperation, T instance, Func<T, T, bool> equalityComparer) where T : class, IOperation
{
// Check every simple assignments target in a statement for equality to `instance`
while (assignmentOperation.Value != null && assignmentOperation.Value.Kind == OperationKind.SimpleAssignment)
{
assignmentOperation = (ISimpleAssignmentOperation)assignmentOperation.Value;
var operationValue = assignmentOperation.Target as T;
if (equalityComparer(instance, operationValue))
{
return true;
}
}
return false;
}
}
}