2
// RedundantFieldInitializerIssue.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;
28
using ICSharpCode.NRefactory.PatternMatching;
29
using ICSharpCode.NRefactory.TypeSystem;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
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
41
public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
43
return new GatherVisitor (context).GetIssues ();
46
class GatherVisitor : GatherVisitorBase<RedundantFieldInitializerIssue>
48
public GatherVisitor(BaseRefactoringContext ctx)
53
public override void VisitFieldDeclaration (FieldDeclaration fieldDeclaration)
55
base.VisitFieldDeclaration (fieldDeclaration);
56
if (fieldDeclaration.HasModifier (Modifiers.Const))
58
var defaultValueExpr = GetDefaultValueExpression (fieldDeclaration.ReturnType);
59
if (defaultValueExpr == null)
62
foreach (var variable1 in fieldDeclaration.Variables) {
63
var variable = variable1;
64
if (!defaultValueExpr.Match (variable.Initializer).Success)
67
AddIssue (variable.Initializer, ctx.TranslateString ("Remove redundant field initializer"),
68
script => script.Replace (variable, new VariableInitializer (variable.Name)));
72
Expression GetDefaultValueExpression (AstType astType)
74
var type = ctx.ResolveType (astType);
76
if ((type.IsReferenceType ?? false) || type.Kind == TypeKind.Dynamic)
77
return new NullReferenceExpression ();
79
var typeDefinition = type.GetDefinition ();
80
if (typeDefinition != null) {
81
switch (typeDefinition.KnownTypeCode) {
82
case KnownTypeCode.Boolean:
83
return new PrimitiveExpression (false);
85
case KnownTypeCode.Char:
86
return new PrimitiveExpression ('\0');
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);
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:
101
new PrimitiveExpression (0), new PrimitiveExpression (0U), new PrimitiveExpression (0UL)
103
case KnownTypeCode.Single:
104
return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0F) };
105
case KnownTypeCode.Double:
107
new PrimitiveExpression (0), new PrimitiveExpression (0F), new PrimitiveExpression (0D)
109
case KnownTypeCode.Decimal:
110
return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0M) };
112
case KnownTypeCode.NullableOfT:
113
return new NullReferenceExpression ();
115
if (type.Kind == TypeKind.Struct)
116
return new ObjectCreateExpression (astType.Clone ());
118
return new DefaultValueExpression (astType.Clone ());