2
// CallToVirtualFunctionFromConstructorIssue.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
using ICSharpCode.NRefactory.CSharp.Refactoring;
27
using System.Collections.Generic;
28
using ICSharpCode.NRefactory.Semantics;
30
namespace ICSharpCode.NRefactory.CSharp.Refactoring
32
[IssueDescription("Constructors should not call virtual members",
33
Description = "Warns about calls to virtual member functions occuring in the constructor.",
34
Category = IssueCategories.CodeQualityIssues,
35
Severity = Severity.Warning,
36
ResharperDisableKeyword = "DoNotCallOverridableMethodsInConstructor")]
37
public class CallToVirtualFunctionFromConstructorIssue : ICodeIssueProvider
39
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
41
var gv = new GatherVisitor(context);
42
context.RootNode.AcceptVisitor(gv);
43
return gv.CallFinder.FoundIssues;
46
class GatherVisitor : GatherVisitorBase<CallToVirtualFunctionFromConstructorIssue>
48
internal readonly VirtualCallFinderVisitor CallFinder;
50
public GatherVisitor(BaseRefactoringContext context) : base (context)
52
CallFinder = new VirtualCallFinderVisitor(context);
57
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
59
if (typeDeclaration.ClassType != ClassType.Class && typeDeclaration.ClassType != ClassType.Struct)
61
bool oldIsSealedType = isSealedType;
62
isSealedType = typeDeclaration.Modifiers.HasFlag(Modifiers.Sealed);
63
base.VisitTypeDeclaration(typeDeclaration);
64
isSealedType = oldIsSealedType;
67
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
71
var body = constructorDeclaration.Body;
72
if (body == null || body.IsNull)
74
body.AcceptVisitor(CallFinder);
77
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
82
public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
87
public override void VisitIndexerExpression(IndexerExpression indexerExpression)
92
public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration)
97
public override void VisitEventDeclaration(EventDeclaration eventDeclaration)
102
public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration)
107
public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration)
113
class VirtualCallFinderVisitor: GatherVisitorBase<CallToVirtualFunctionFromConstructorIssue>
115
readonly BaseRefactoringContext context;
117
public VirtualCallFinderVisitor(BaseRefactoringContext context) : base(context)
119
this.context = context;
122
public override void VisitInvocationExpression(InvocationExpression invocationExpression)
124
base.VisitInvocationExpression(invocationExpression);
125
if (!IsCallDependentOnCurrentInstance(invocationExpression))
126
// Call within current class scope without 'this' or 'base'
128
var targetMethod = context.Resolve(invocationExpression) as InvocationResolveResult;
129
if (targetMethod == null)
131
if (targetMethod.IsVirtualCall) {
132
AddIssue(invocationExpression,
133
context.TranslateString("Constructors should not call virtual members"));
137
bool IsCallDependentOnCurrentInstance(InvocationExpression invocationExpression)
139
if (invocationExpression.Target is IdentifierExpression)
140
// Call within current class scope without 'this' or 'base'
142
var expression = invocationExpression.Target as MemberReferenceExpression;
143
if (expression == null || expression.Target is ThisReferenceExpression)
144
// Call within current class scope using 'this' or 'base'
149
public override void VisitLambdaExpression(LambdaExpression lambdaExpression)
154
public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression)
156
// ignore anonymous methods