IG: remove duplication
authorBas Leijdekkers <basleijdekkers@gmail.com>
Wed, 21 Sep 2016 18:48:38 +0000 (20:48 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Thu, 22 Sep 2016 16:52:23 +0000 (18:52 +0200)
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java

index 28f7081fb0d0e19eb2613f961d8a88842fc978e9..ea1524c1620c1c0954c094b446f1c2d1178a1262 100644 (file)
@@ -152,22 +152,23 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection imp
         }
       }
       if (argument instanceof PsiReferenceExpression) {
-        final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)argument;
-        final PsiElement target = referenceExpression.resolve();
-        if (!(target instanceof PsiModifierListOwner) || !NullableNotNullManager.isNotNull((PsiModifierListOwner)target)) {
-          // don't warn because unwrapping when null would change semantics
+        if (couldChangeSemantics((PsiReferenceExpression)argument)) {
           return;
         }
       }
-      else if (argument instanceof PsiMethodCallExpression){
+      else if (argument instanceof PsiMethodCallExpression) {
         final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)argument;
-        final PsiMethod method1 = methodCallExpression.resolveMethod();
-        if (method1 == null || !NullableNotNullManager.isNotNull(method1)) {
-          // don't warn because unwrapping when null would change semantics
+        if (couldChangeSemantics(methodCallExpression.getMethodExpression())) {
           return;
         }
       }
       registerError(expression, calculateReplacementText(argument));
     }
+
+    private static boolean couldChangeSemantics(PsiReferenceExpression referenceExpression) {
+      final PsiElement target = referenceExpression.resolve();
+      // unwrapping when null will change semantics
+      return !(target instanceof PsiModifierListOwner && NullableNotNullManager.isNotNull((PsiModifierListOwner)target));
+    }
   }
 }