コード例 #1
0
 public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
 {
     foreach (var node in constructorDeclaration.Descendants)
     {
         if (node is AnonymousMethodExpression || node is LambdaExpression)
         {
             node.AcceptVisitor(this);
         }
         else
         {
             var assignmentAnalysis = new ConvertToConstantIssue.VariableUsageAnalyzation(ctx);
             var newVars            = new List <Tuple <VariableInitializer, IVariable, VariableState> >();
             node.AcceptVisitor(assignmentAnalysis);
             foreach (var variable in fieldStack.Pop())
             {
                 var state = assignmentAnalysis.GetStatus(variable.Item2);
                 if (variable.Item3 > state)
                 {
                     state = variable.Item3;
                 }
                 newVars.Add(new Tuple <VariableInitializer, IVariable, VariableState> (variable.Item1, variable.Item2, state));
             }
             fieldStack.Push(newVars);
         }
     }
 }
コード例 #2
0
			public override void VisitBlockStatement(BlockStatement blockStatement)
			{
				var assignmentAnalysis = new ConvertToConstantIssue.VariableUsageAnalyzation (ctx);
				var newVars = new List<Tuple<VariableInitializer, IVariable, VariableState>>();
				blockStatement.AcceptVisitor(assignmentAnalysis); 
					foreach (var variable in fieldStack.Pop()) {
						var state = assignmentAnalysis.GetStatus(variable.Item2);
						if (state == VariableState.Changed)
							continue;
						newVars.Add(new Tuple<VariableInitializer, IVariable, VariableState> (variable.Item1, variable.Item2, state));
					}
					fieldStack.Push(newVars);
			}
コード例 #3
0
            public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression)
            {
                base.VisitAnonymousMethodExpression(anonymousMethodExpression);
                if (!anonymousMethodExpression.HasParameterList || !IsParameterListRedundant(anonymousMethodExpression))
                {
                    return;
                }

                var parameters = anonymousMethodExpression.Parameters.ToList();

                if (parameters.Count > 0)
                {
                    var usageAnalysis = new ConvertToConstantIssue.VariableUsageAnalyzation(ctx);
                    anonymousMethodExpression.Body.AcceptVisitor(usageAnalysis);
                    foreach (var parameter in parameters)
                    {
                        var rr = ctx.Resolve(parameter) as LocalResolveResult;
                        if (rr == null)
                        {
                            continue;
                        }
                        if (usageAnalysis.GetStatus(rr.Variable) != ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod.VariableState.None)
                        {
                            return;
                        }
                    }
                }

                AddIssue(new CodeIssue(anonymousMethodExpression.LParToken.StartLocation,
                                       anonymousMethodExpression.RParToken.EndLocation,
                                       ctx.TranslateString("Specifying signature is redundant because no parameter is used"),
                                       ctx.TranslateString("Remove redundant signature"),
                                       script => {
                    int start = script.GetCurrentOffset(anonymousMethodExpression.DelegateToken.EndLocation);
                    int end   = script.GetCurrentOffset(anonymousMethodExpression.Body.StartLocation);

                    script.Replace(start, end - start, " ");
                })
                {
                    IssueMarker = IssueMarker.GrayOut
                });
            }
コード例 #4
0
            public override void VisitForStatement(ForStatement forStatement)
            {
                base.VisitForStatement(forStatement);
                var forMatch = forPattern.Match(forStatement);

                if (!forMatch.Success)
                {
                    return;
                }
                var body = forStatement.EmbeddedStatement as BlockStatement;

                if (body == null || !body.Statements.Any())
                {
                    return;
                }
                var varDeclStmt = body.Statements.First() as VariableDeclarationStatement;

                if (varDeclStmt == null)
                {
                    return;
                }
                var varMatch = varDeclPattern.Match(varDeclStmt);

                if (!varMatch.Success)
                {
                    return;
                }
                var typeNode   = forMatch.Get <AstNode>("int").FirstOrDefault();
                var varDecl    = forMatch.Get <VariableInitializer>("iteratorInitialzer").FirstOrDefault();
                var iterator   = forMatch.Get <IdentifierExpression>("iterator").FirstOrDefault();
                var upperBound = forMatch.Get <MemberReferenceExpression>("upperBound").FirstOrDefault();

                if (typeNode == null || varDecl == null || iterator == null || upperBound == null)
                {
                    return;
                }

                // Check iterator type
                if (!varTypePattern.IsMatch(typeNode))
                {
                    var typeRR = ctx.Resolve(typeNode);
                    if (!typeRR.Type.IsKnownType(KnownTypeCode.Int32))
                    {
                        return;
                    }
                }

                if (varDecl.Name != iterator.Identifier)
                {
                    return;
                }

                var upperBoundInitializer     = forMatch.Get <VariableInitializer>("upperBoundInitializer").FirstOrDefault();
                var upperBoundInitializerName = forMatch.Get <IdentifierExpression>("upperBoundInitializerName").FirstOrDefault();

                if (upperBoundInitializer != null)
                {
                    if (upperBoundInitializerName == null || upperBoundInitializer.Name != upperBoundInitializerName.Identifier)
                    {
                        return;
                    }
                }

                var indexer = varMatch.Get <IndexerExpression>("indexer").Single();

                if (((IdentifierExpression)indexer.Arguments.First()).Identifier != iterator.Identifier)
                {
                    return;
                }
                if (!indexer.Target.IsMatch(upperBound.Target))
                {
                    return;
                }

                var rr = ctx.Resolve(upperBound) as MemberResolveResult;

                if (rr == null || rr.IsError)
                {
                    return;
                }

                if (!(rr.Member.Name == "Length" && rr.Member.DeclaringType.Name == "Array" && rr.Member.DeclaringType.Namespace == "System") &&
                    !(rr.Member.Name == "Count" && (IsEnumerable(rr.TargetResult.Type) || rr.TargetResult.Type.GetAllBaseTypes().Any(IsEnumerable))))
                {
                    return;
                }

                var variableInitializer = varDeclStmt.Variables.First();
                var lr = ctx.Resolve(variableInitializer) as LocalResolveResult;

                if (lr == null)
                {
                    return;
                }

                var ir = ctx.Resolve(varDecl) as LocalResolveResult;

                if (ir == null)
                {
                    return;
                }

                var analyze = new ConvertToConstantIssue.VariableUsageAnalyzation(ctx);

                analyze.SetAnalyzedRange(
                    varDeclStmt,
                    forStatement.EmbeddedStatement,
                    false
                    );
                forStatement.EmbeddedStatement.AcceptVisitor(analyze);
                if (analyze.GetStatus(lr.Variable) == ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod.VariableState.Changed ||
                    analyze.GetStatus(ir.Variable) == ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod.VariableState.Changed ||
                    analyze.GetStatus(ir.Variable) == ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod.VariableState.Used)
                {
                    return;
                }

                AddIssue(new CodeIssue(
                             forStatement.ForToken,
                             ctx.TranslateString("'for' loop can be converted to 'foreach'"),
                             ctx.TranslateString("Convert to 'foreach'"),
                             script => {
                    var foreachBody = (BlockStatement)forStatement.EmbeddedStatement.Clone();
                    foreachBody.Statements.First().Remove();

                    var fe = new ForeachStatement {
                        VariableType      = new PrimitiveType("var"),
                        VariableName      = variableInitializer.Name,
                        InExpression      = upperBound.Target.Clone(),
                        EmbeddedStatement = foreachBody
                    };
                    script.Replace(forStatement, fe);
                }
                             )
                {
                    IssueMarker = IssueMarker.DottedLine
                });
            }