-
Notifications
You must be signed in to change notification settings - Fork 294
/
MisleadingByRefParameterInspection.cs
68 lines (65 loc) · 2.75 KB
/
MisleadingByRefParameterInspection.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
using Rubberduck.CodeAnalysis.Inspections.Abstract;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.VBA.DeclarationCaching;
using Rubberduck.Resources.Inspections;
using System.Linq;
namespace Rubberduck.CodeAnalysis.Inspections.Concrete
{
/// <summary>
/// Flags the value-parameter of a property mutators that are declared with an explict ByRef modifier.
/// </summary>
/// <why>
/// Regardless of the presence or absence of an explicit ByRef or ByVal modifier, the value-parameter
/// of a property mutator is always treated as though it had an explicit ByVal modifier.
/// Exception: UserDefinedType parameters are always passed by reference.
/// </why>
/// <example hasResult="true">
/// <module name="MyModule" type="Standard Module">
/// <![CDATA[
/// Private fizzField As Long
/// Public Property Get Fizz() As Long
/// Fizz = fizzFiled
/// End Property
/// Public Property Let Fizz(ByRef arg As Long)
/// fizzFiled = arg
/// End Property
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="MyModule" type="Standard Module">
/// <![CDATA[
/// Private fizzField As Long
/// Public Property Get Fizz() As Long
/// Fizz = fizzFiled
/// End Property
/// Public Property Let Fizz(arg As Long)
/// fizzFiled = arg
/// End Property
/// ]]>
/// </module>
/// </example>
internal sealed class MisleadingByRefParameterInspection : DeclarationInspectionBase
{
public MisleadingByRefParameterInspection(IDeclarationFinderProvider declarationFinderProvider)
: base(declarationFinderProvider, DeclarationType.Parameter)
{ }
protected override bool IsResultDeclaration(Declaration declaration, DeclarationFinder finder)
{
return declaration is ParameterDeclaration parameter
&& !(parameter.AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.UserDefinedType) ?? false)
&& parameter.ParentDeclaration is ModuleBodyElementDeclaration enclosingMethod
&& (enclosingMethod.DeclarationType.HasFlag(DeclarationType.PropertyLet)
|| enclosingMethod.DeclarationType.HasFlag(DeclarationType.PropertySet))
&& enclosingMethod.Parameters.Last() == parameter
&& parameter.IsByRef && !parameter.IsImplicitByRef;
}
protected override string ResultDescription(Declaration declaration)
{
return string.Format(
InspectionResults.MisleadingByRefParameterInspection,
declaration.IdentifierName, declaration.ParentDeclaration.QualifiedName.MemberName);
}
}
}