~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantFieldInitializerIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// RedundantFieldInitializerIssue.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System.Collections.Generic;
 
28
using ICSharpCode.NRefactory.PatternMatching;
 
29
using ICSharpCode.NRefactory.TypeSystem;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
32
{
 
33
        [IssueDescription ("Initializing field with default value is redundant",
 
34
                                                Description = "Initializing field with default value is redundant.",
 
35
                                                Category = IssueCategories.Redundancies,
 
36
                                                Severity = Severity.Hint,
 
37
                                                IssueMarker = IssueMarker.GrayOut,
 
38
                        ResharperDisableKeyword = "RedundantDefaultFieldInitializer")]
 
39
        public class RedundantFieldInitializerIssue : ICodeIssueProvider
 
40
        {
 
41
                public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
 
42
                {
 
43
                        return new GatherVisitor (context).GetIssues ();
 
44
                }
 
45
 
 
46
                class GatherVisitor : GatherVisitorBase<RedundantFieldInitializerIssue>
 
47
                {
 
48
                        public GatherVisitor(BaseRefactoringContext ctx)
 
49
                                : base(ctx)
 
50
                        {
 
51
                        }
 
52
 
 
53
                        public override void VisitFieldDeclaration (FieldDeclaration fieldDeclaration)
 
54
                        {
 
55
                                base.VisitFieldDeclaration (fieldDeclaration);
 
56
                                if (fieldDeclaration.HasModifier (Modifiers.Const))
 
57
                                        return;
 
58
                                var defaultValueExpr = GetDefaultValueExpression (fieldDeclaration.ReturnType);
 
59
                                if (defaultValueExpr == null)
 
60
                                        return;
 
61
 
 
62
                                foreach (var variable1 in fieldDeclaration.Variables) {
 
63
                                        var variable = variable1;
 
64
                                        if (!defaultValueExpr.Match (variable.Initializer).Success)
 
65
                                                continue;
 
66
 
 
67
                                        AddIssue (variable.Initializer, ctx.TranslateString ("Remove redundant field initializer"),
 
68
                                                script => script.Replace (variable, new VariableInitializer (variable.Name)));
 
69
                                }
 
70
                        }
 
71
 
 
72
                        Expression GetDefaultValueExpression (AstType astType)
 
73
                        {
 
74
                                var type = ctx.ResolveType (astType);
 
75
 
 
76
                                if ((type.IsReferenceType ?? false) || type.Kind == TypeKind.Dynamic)
 
77
                                        return new NullReferenceExpression ();
 
78
 
 
79
                                var typeDefinition = type.GetDefinition ();
 
80
                                if (typeDefinition != null) {
 
81
                                        switch (typeDefinition.KnownTypeCode) {
 
82
                                                case KnownTypeCode.Boolean:
 
83
                                                        return new PrimitiveExpression (false);
 
84
 
 
85
                                                case KnownTypeCode.Char:
 
86
                                                        return new PrimitiveExpression ('\0');
 
87
 
 
88
                                                case KnownTypeCode.SByte:
 
89
                                                case KnownTypeCode.Byte:
 
90
                                                case KnownTypeCode.Int16:
 
91
                                                case KnownTypeCode.UInt16:
 
92
                                                case KnownTypeCode.Int32:
 
93
                                                        return new PrimitiveExpression (0);
 
94
 
 
95
                                                case KnownTypeCode.Int64:
 
96
                                                        return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0L) };
 
97
                                                case KnownTypeCode.UInt32:
 
98
                                                        return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0U) };
 
99
                                                case KnownTypeCode.UInt64:
 
100
                                                        return new Choice {
 
101
                                                                new PrimitiveExpression (0), new PrimitiveExpression (0U), new PrimitiveExpression (0UL)
 
102
                                                        };
 
103
                                                case KnownTypeCode.Single:
 
104
                                                        return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0F) };
 
105
                                                case KnownTypeCode.Double:
 
106
                                                        return new Choice {
 
107
                                                                new PrimitiveExpression (0), new PrimitiveExpression (0F), new PrimitiveExpression (0D)
 
108
                                                        };
 
109
                                                case KnownTypeCode.Decimal:
 
110
                                                        return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0M) };
 
111
 
 
112
                                                case KnownTypeCode.NullableOfT:
 
113
                                                        return new NullReferenceExpression ();
 
114
                                        }
 
115
                                        if (type.Kind == TypeKind.Struct)
 
116
                                                return new ObjectCreateExpression (astType.Clone ());
 
117
                                }
 
118
                                return new DefaultValueExpression (astType.Clone ());
 
119
                        } 
 
120
                        
 
121
                }
 
122
        }
 
123
}