2
// AccessToModifiedClosureIssue.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
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
28
using System.Collections.Generic;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
//[IssueDescription("Access to modified closure variable",
34
// Description = "Access to closure variable from anonymous method when the variable is modified " +
36
// Category = IssueCategories.CodeQualityIssues,
37
// Severity = Severity.Warning,
38
// IssueMarker = IssueMarker.Underline,
39
// ResharperDisableKeyword = "AccessToModifiedClosure")]
40
public class AccessToModifiedClosureIssue : AccessToClosureIssue
42
public AccessToModifiedClosureIssue ()
43
: base ("Access to modified closure")
47
protected override NodeKind GetNodeKind (AstNode node)
49
var assignment = node.GetParent<AssignmentExpression> ();
50
if (assignment != null && assignment.Left == node) {
51
if (assignment.Operator == AssignmentOperatorType.Assign)
52
return NodeKind.Modification;
53
return NodeKind.ReferenceAndModification;
55
var unaryExpr = node.GetParent<UnaryOperatorExpression> ();
56
if (unaryExpr != null && unaryExpr.Expression == node &&
57
(unaryExpr.Operator == UnaryOperatorType.Increment ||
58
unaryExpr.Operator == UnaryOperatorType.PostIncrement ||
59
unaryExpr.Operator == UnaryOperatorType.Decrement ||
60
unaryExpr.Operator == UnaryOperatorType.PostDecrement)) {
61
return NodeKind.ReferenceAndModification;
63
if (node.Parent is ForeachStatement)
64
return NodeKind.Modification;
66
return NodeKind.Reference;
69
protected override IEnumerable<CodeAction> GetFixes (BaseRefactoringContext context, Node env,
72
var containingStatement = env.ContainingStatement;
74
// we don't give a fix for these cases since the general fix may not work
75
// lambda in while/do-while/for condition
76
if (containingStatement is WhileStatement || containingStatement is DoWhileStatement ||
77
containingStatement is ForStatement)
79
// lambda in for initializer/iterator
80
if (containingStatement.Parent is ForStatement &&
81
((ForStatement)containingStatement.Parent).EmbeddedStatement != containingStatement)
84
Action<Script> action = script =>
86
var newName = LocalVariableNamePicker.PickSafeName (
87
containingStatement.GetParent<EntityDeclaration> (),
88
Enumerable.Range (1, 100).Select (i => variableName + i));
90
var variableDecl = new VariableDeclarationStatement (new SimpleType("var"), newName,
91
new IdentifierExpression (variableName));
93
if (containingStatement.Parent is BlockStatement || containingStatement.Parent is SwitchSection) {
94
script.InsertBefore (containingStatement, variableDecl);
96
var offset = script.GetCurrentOffset (containingStatement.StartLocation);
97
script.InsertBefore (containingStatement, variableDecl);
98
script.InsertText (offset, "{");
99
script.InsertText (script.GetCurrentOffset (containingStatement.EndLocation), "}");
100
script.FormatText (containingStatement.Parent);
103
var textNodes = new List<AstNode> ();
104
textNodes.Add (variableDecl.Variables.First ().NameToken);
106
foreach (var reference in env.GetAllReferences ()) {
107
var identifier = new IdentifierExpression (newName);
108
script.Replace (reference.AstNode, identifier);
109
textNodes.Add (identifier);
111
script.Link (textNodes.ToArray ());
113
yield return new CodeAction (context.TranslateString ("Copy to local variable"), action, env.AstNode);