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

DontIgnoreMethodResultRule.cs « Gendarme.Rules.Performance « rules « gendarme - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8736b3f6a745cf2139549bc73e15d94e058f08d (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
//
// Gendarme.Rules.Performance.DontIgnoreMethodResultRule
//
// Authors:
//	Lukasz Knop <lukasz.knop@gmail.com>
//	Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2007 Lukasz Knop
// Copyright (C) 2007-2008 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 Mono.Cecil;
using Mono.Cecil.Cil;

using Gendarme.Framework;
using Gendarme.Framework.Engines;
using Gendarme.Framework.Helpers;
using Gendarme.Framework.Rocks;

namespace Gendarme.Rules.Performance {

	/// <summary>
	/// This rule fires if a method is called that returns a new instance but that instance
	/// is not used. This is a performance problem because it is wasteful to create and
	/// collect objects which are never actually used. It may also indicate a logic problem.
	/// Note that this rule currently only checks methods within a small number of System
	/// types.
	/// </summary>
	/// <example>
	/// Bad example:
	/// <code>
	/// public void GetName ()
	/// {
	///	string name = Console.ReadLine ();
	///	// This is a bug: strings are (mostly) immutable so Trim leaves
	/// 	// name untouched and returns a new string.
	///	name.Trim ();
	///	Console.WriteLine ("Name: {0}", name);
	/// }
	/// </code>
	/// </example>
	/// <example>
	/// Good example:
	/// <code>
	/// public void GetName ()
	/// {
	///	string name = Console.ReadLine ();
	///	name = name.Trim ();
	///	Console.WriteLine ("Name: {0}", name);
	/// }
	/// </code>
	/// </example>

	[Problem ("The method ignores the result value from a method call.")]
	[Solution ("Don't ignore the result value.")]
	[EngineDependency (typeof (OpCodeEngine))]
	[FxCopCompatibility ("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults")]
	public class DoNotIgnoreMethodResultRule : Rule, IMethodRule {

		public RuleResult CheckMethod (MethodDefinition method)
		{
			// rule only applies if the method has a body
			// rule doesn't not apply to generated code (out of developer's control)
			if (!method.HasBody || method.IsGeneratedCode ())
				return RuleResult.DoesNotApply;

			// check if the method contains a Pop instruction
			if (!OpCodeEngine.GetBitmask (method).Get (Code.Pop))
				return RuleResult.DoesNotApply;

			foreach (Instruction instruction in method.Body.Instructions) {
				if (instruction.OpCode.Code == Code.Pop) {
					CheckForViolation (method, instruction.Previous);
				}
			}
			return Runner.CurrentRuleResult;
		}

		private static bool IsCallException (MethodReference method)
		{
			switch (method.DeclaringType.FullName) {
			case "System.String":
				// Since strings are immutable, calling System.String methods that returns strings 
				// better be assigned to something
				return (method.ReturnType.ReturnType.FullName != "System.String");
			case "System.IO.DirectoryInfo":
				// GetDirectories overloads don't apply to the instance
				return (method.Name != "GetDirectories");
			case "System.Security.PermissionSet":
				// Intersection and Union returns a new PermissionSet (it does not change the instance)
				return (method.ReturnType.ReturnType.FullName != "System.Security.PermissionSet");
			default:
				// this is useless anytime, if unassigned, more in cases like a StringBuilder
				return (method.Name != "ToString");
			}
		}

		private static bool IsNewException (MemberReference method)
		{
			switch (method.ToString ()) {
			// supplying a callback is enough to make the Timer creation worthwhile
			case "System.Void System.Threading.Timer::.ctor(System.Threading.TimerCallback,System.Object,System.Int32,System.Int32)":
				return true;
			default:
				return false;
			}
		}

		private void CheckForViolation (MethodDefinition method, Instruction instruction)
		{
			if ((instruction.OpCode.Code == Code.Newobj || instruction.OpCode.Code == Code.Newarr)) {
				MemberReference member = (instruction.Operand as MemberReference);
				if ((member != null) && !IsNewException (member)) {
					string s = String.Format ("Unused object of type '{0}' created.", member.ToString ());
					Runner.Report (method, instruction, Severity.High, Confidence.Normal, s);
				}
			}

			if (instruction.OpCode.Code == Code.Call || instruction.OpCode.Code == Code.Callvirt) {
				MethodReference callee = instruction.Operand as MethodReference;
				if (callee != null && !callee.ReturnType.ReturnType.IsValueType) {
					// check for some common exceptions (to reduce false positive)
					if (!IsCallException (callee)) {
						string s = String.Format ("Do not ignore method results from call to '{0}'.", callee.ToString ());
						Runner.Report (method, instruction, Severity.Medium, Confidence.Normal, s);
					}
				}
			}
		}
	}
}