Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AvoidRefAndOutParametersRule.cs « Gendarme.Rules.Design « rules « gendarme - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e08ab2948fccada9b3d75704cab8522be67b74f (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Gendarme.Rules.Design.AvoidRefAndOutParametersRule
//
// Authors:
//	Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2008, 2011 Novell, Inc (http://www.novell.com)
//
// 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.Globalization;
using Mono.Cecil;

using Gendarme.Framework;
using Gendarme.Framework.Rocks;

namespace Gendarme.Rules.Design {

	/// <summary>
	/// This rule fires if a method uses <c>ref</c> or <c>out</c> parameters. 
	/// These are advanced features that can easily be misunderstood and misused 
	/// (by the consumer). This can result in an API that is difficult to use so
	/// avoid them whenever possible. 
	/// An alternative is to use one of the new generic <c>Tuple</c> types (FX v4 and later)
	/// as the return value.
	/// No defect will be reported if the method:
	/// <list>
	/// <item>follows the <c>bool Try*(X out)</c> pattern; or</item>
	/// <item>implement an interface requiring the use of <c>ref</c> or <c>out</c> parameters.
	/// In the later case defects will be reported against the interface itself (if analyzed
	/// by Gendarme).</item>
	/// </list>
	/// </summary>
	/// <example>
	/// Bad example:
	/// <code>
	/// public bool NextJob (ref int id, out string display)
	/// {
	///	if (id &lt; 0)
	///		return false;
	///	display = String.Format ("Job #{0}", id++);
	///	return true;
	/// }
	/// </code>
	/// </example>
	/// <example>
	/// Good example:
	/// <code>
	/// private int id = 0;
	/// 
	/// private int GetNextId ()
	/// {
	///	int id = this.id++;
	///	return id;
	/// }
	/// 
	/// public string NextJob ()
	/// {
	///	return String.Format ("Job #{0}", Id);
	/// }
	/// </code>
	/// </example>
	/// <remarks>This rule is available since Gendarme 2.0</remarks>

	[Problem ("This method use ref and/or out parameters in a visible API which can confuse many developers.")]
	[Solution ("The most common reason to do this is to return multiple values from a method which can be rewritten so that it returns a custom type instead.")]
	[FxCopCompatibility ("Microsoft.Design", "CA1021:AvoidOutParameters")]
	[FxCopCompatibility ("Microsoft.Design", "CA1045:DoNotPassTypesByReference")]
	public class AvoidRefAndOutParametersRule : Rule, IMethodRule {

		static bool IsSignatureDictatedByInterface (MethodDefinition method, TypeReference type)
		{
			if (type == null)
				return false;

			TypeDefinition td = type.Resolve ();
			if (td == null)
				return false;

			foreach (InterfaceImplementation intf_ref in td.Interfaces) {
				TypeDefinition intr = intf_ref.InterfaceType.Resolve ();
				if (intr == null)
					continue;

				if (intr.HasMethods) {
					foreach (MethodDefinition md in intr.Methods) {
						if (method.CompareSignature (md))
							return true;
					}
				}
				// check if the interface inherits from another interface with this signature
				if (IsSignatureDictatedByInterface (method, intr.BaseType))
					return true;
			}
			// check if a base type is implementing an interface with this signature
			return IsSignatureDictatedByInterface (method, td.BaseType);
		}

		static bool IsSignatureDictatedByInterface (MethodDefinition method)
		{
			// quick-out: if the method implements an interface then it's virtual
			if (!method.IsVirtual)
				return false;

			// check if the interface itself has the defect (always reported)
			TypeDefinition type = method.DeclaringType;
			if (type.IsInterface)
				return false;

			return IsSignatureDictatedByInterface (method, type);
		}

		public RuleResult CheckMethod (MethodDefinition method)
		{
			// rule only applies to visible methods with parameters
			// we also exclude all p/invokes since we have a rule for them not to be visible
			if (method.IsPInvokeImpl || !method.HasParameters || !method.IsVisible ())
				return RuleResult.DoesNotApply;

			foreach (ParameterDefinition parameter in method.Parameters) {
				string how = null;
				if (parameter.IsOut) {
					// out is permitted for the "bool Try* (...)" pattern
					if (method.ReturnType.IsNamed ("System", "Boolean") && 
						method.Name.StartsWith ("Try", StringComparison.Ordinal)) {
						continue;
					}

					how = "out";
				} else if (parameter.ParameterType.IsByReference) {
					how = "ref";
				}

				// report the method is it is not dictated by an interface
				if ((how != null) && !IsSignatureDictatedByInterface (method)) {
					// goal is to keep the API as simple as possible so this is more severe for public than protected methods
					Severity severity = method.IsPublic ? Severity.Medium : Severity.Low;
					string msg = String.Format (CultureInfo.InvariantCulture,
						"Parameter '{0}' passed by reference ({1}).", parameter.Name, how);
					Runner.Report (parameter, severity, Confidence.Total, msg);
				}
			}

			return Runner.CurrentRuleResult;
		}
	}
}