2
// AccessToDisposedClosureIssue.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
27
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.CSharp.Analysis;
30
using ICSharpCode.NRefactory.TypeSystem;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
35
//[IssueDescription ("Access to disposed closure variable",
36
// Description = "Access to closure variable from anonymous method when the variable is" +
37
// " disposed externally",
38
// Category = IssueCategories.CodeQualityIssues,
39
// Severity = Severity.Warning,
40
// IssueMarker = IssueMarker.Underline,
41
// ResharperDisableKeyword = "AccessToDisposedClosure")]
42
public class AccessToDisposedClosureIssue : AccessToClosureIssue
44
public AccessToDisposedClosureIssue ()
45
: base ("Access to disposed closure")
49
protected override bool IsTargetVariable (IVariable variable)
51
return variable.Type.GetAllBaseTypeDefinitions ().Any (t => t.KnownTypeCode == KnownTypeCode.IDisposable);
54
protected override NodeKind GetNodeKind (AstNode node)
56
if (node.Parent is UsingStatement)
57
return NodeKind.ReferenceAndModification;
59
if (node.Parent is VariableDeclarationStatement && node.Parent.Parent is UsingStatement)
60
return NodeKind.Modification;
62
var memberRef = node.Parent as MemberReferenceExpression;
63
if (memberRef != null && memberRef.Parent is InvocationExpression && memberRef.MemberName == "Dispose")
64
return NodeKind.ReferenceAndModification;
66
return NodeKind.Reference;
69
protected override bool CanReachModification (ControlFlowNode node, Statement start,
70
IDictionary<Statement, IList<Node>> modifications)
72
if (base.CanReachModification (node, start, modifications))
75
if (node.NextStatement != start) {
76
var usingStatement = node.PreviousStatement as UsingStatement;
77
if (usingStatement != null) {
78
if (modifications.ContainsKey(usingStatement))
80
if (usingStatement.ResourceAcquisition is Statement &&
81
modifications.ContainsKey ((Statement)usingStatement.ResourceAcquisition))
88
protected override IEnumerable<CodeAction> GetFixes (BaseRefactoringContext context, Node env,
b'\\ No newline at end of file'