IDEADEV-31824 (Incorrect "manual array copy" warning)
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / psiutils / VariableAssignedVisitor.java
blobb6777b450a3d83760e79c790b4a0068e033766d6
1 /*
2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.siyeh.ig.psiutils;
18 import com.intellij.psi.*;
19 import com.intellij.psi.tree.IElementType;
20 import org.jetbrains.annotations.NotNull;
22 public class VariableAssignedVisitor extends JavaRecursiveElementVisitor{
24 @NotNull private final PsiVariable variable;
25 private final boolean recurseIntoClasses;
26 private boolean assigned = false;
28 public VariableAssignedVisitor(@NotNull PsiVariable variable,
29 boolean recurseIntoClasses){
30 super();
31 this.variable = variable;
32 this.recurseIntoClasses = recurseIntoClasses;
35 @Override public void visitElement(@NotNull PsiElement element){
36 if(!assigned){
37 super.visitElement(element);
41 @Override public void visitAssignmentExpression(
42 @NotNull PsiAssignmentExpression assignment){
43 if(assigned){
44 return;
46 super.visitAssignmentExpression(assignment);
47 final PsiExpression arg = assignment.getLExpression();
48 if(VariableAccessUtils.mayEvaluateToVariable(arg, variable)){
49 assigned = true;
53 @Override
54 public void visitClass(PsiClass aClass) {
55 if(!recurseIntoClasses){
56 return;
58 if(assigned){
59 return;
61 super.visitClass(aClass);
64 @Override public void visitPrefixExpression(
65 @NotNull PsiPrefixExpression prefixExpression){
66 if(assigned){
67 return;
69 super.visitPrefixExpression(prefixExpression);
70 final PsiJavaToken operationSign = prefixExpression.getOperationSign();
71 final IElementType tokenType = operationSign.getTokenType();
72 if(!tokenType.equals(JavaTokenType.PLUSPLUS) &&
73 !tokenType.equals(JavaTokenType.MINUSMINUS)){
74 return;
76 final PsiExpression operand = prefixExpression.getOperand();
77 if(VariableAccessUtils.mayEvaluateToVariable(operand, variable)){
78 assigned = true;
82 @Override public void visitPostfixExpression(
83 @NotNull PsiPostfixExpression postfixExpression){
84 if(assigned){
85 return;
87 super.visitPostfixExpression(postfixExpression);
88 final PsiJavaToken operationSign = postfixExpression.getOperationSign();
89 final IElementType tokenType = operationSign.getTokenType();
90 if(!tokenType.equals(JavaTokenType.PLUSPLUS) &&
91 !tokenType.equals(JavaTokenType.MINUSMINUS)){
92 return;
94 final PsiExpression operand = postfixExpression.getOperand();
95 if(VariableAccessUtils.mayEvaluateToVariable(operand, variable)){
96 assigned = true;
100 public boolean isAssigned(){
101 return assigned;