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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
//
// Gendarme.Rules.Maintainability.VariableNamesShouldNotMatchFieldNamesRule
//
// Authors:
// N Lum <nol888@gmail.com>
//
// Copyright (C) 2010 N Lum
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Gendarme.Framework;
using Gendarme.Framework.Rocks;
namespace Gendarme.Rules.Maintainability {
/// <summary>
/// This rule checks for local variables or parameters whose names match (case sensitive) an instance field name.
/// Note that variable names can only be verified when debugging symbols (pdb or mdb) are available.
/// </summary>
/// <example>
/// Bad example:
/// <code>
/// public class Bad {
/// public int value;
///
/// public void DoSomething (int value)
/// {
/// // without 'this.' the field will never be set
/// this.value = value;
/// }
/// }
/// </code>
/// </example>
/// <example>
/// Good example:
/// <code>
/// public class Good {
/// public int value;
///
/// public void DoSomething (int integralValue)
/// {
/// value = integralValue;
/// }
/// }
/// </code>
/// </example>
[Problem ("An instance method declares a parameter or a local variable whose name matches an instance field of the declaring type.")]
[Solution ("Rename the variable/parameter or the field.")]
[FxCopCompatibility ("Microsoft.Maintainability", "CA1500:VariableNamesShouldNotMatchFieldNames")]
public class VariableNamesShouldNotMatchFieldNamesRule : Rule, ITypeRule {
// Storing all field names in a hashset provides quicker .Contains(), and saves time
// in the long run.
HashSet<string> fields;
public VariableNamesShouldNotMatchFieldNamesRule ()
{
fields = new HashSet<string> ();
}
public RuleResult CheckType (TypeDefinition type)
{
// We only like types with fields AND methods.
if (!type.HasFields || !type.HasMethods || type.IsGeneratedCode ())
return RuleResult.DoesNotApply;
fields.Clear ();
foreach (FieldDefinition field in type.Fields)
fields.Add (field.Name);
// Iterate through all the methods. Check parameter names then method bodies.
foreach (MethodDefinition method in type.Methods) {
if (method.HasParameters) {
foreach (ParameterDefinition param in method.Parameters) {
if (fields.Contains (param.Name))
Runner.Report (param, Severity.Medium, Confidence.Total);
}
}
// Method bodies w/o variables don't interest me.
if (!method.HasBody)
continue;
MethodBody body = method.Body;
if (body.HasVariables) {
// Iterate through all variables in the method body.
foreach (VariableDefinition var in body.Variables) {
// if the name is compiler generated or if we do not have debugging symbols...
if (var.IsGeneratedName ())
continue;
// var.Name is not valid anymore since Cecil 0.10
//if (fields.Contains (var.Name))
// Runner.Report (method, Severity.Medium, Confidence.Normal, var.Name);
}
}
}
return Runner.CurrentRuleResult;
}
}
}
|