From 415a04e4b626e0c227a6676e3380da91f5c3cac8 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 17 Oct 2017 18:49:53 +0200 Subject: [PATCH] IG: autoboxing booleans and bytes is not object creation (IDEA-180676) --- ...stantiationInEqualsHashCodeInspection.java | 23 +++++++------------ .../ObjectInstantiationInEqualsHashCode.java | 9 ++++++++ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ObjectInstantiationInEqualsHashCodeInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ObjectInstantiationInEqualsHashCodeInspection.java index 4295369b3160..ebad27075f08 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ObjectInstantiationInEqualsHashCodeInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ObjectInstantiationInEqualsHashCodeInspection.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.siyeh.ig.performance; import com.intellij.psi.*; @@ -20,8 +6,10 @@ import com.intellij.psi.util.PsiTreeUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.psiutils.ExpectedTypeUtils; import com.siyeh.ig.psiutils.ExpressionUtils; import com.siyeh.ig.psiutils.MethodUtils; +import com.siyeh.ig.psiutils.TypeUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -60,6 +48,11 @@ public class ObjectInstantiationInEqualsHashCodeInspection extends BaseInspectio if (!ExpressionUtils.isAutoBoxed(expression) || !isInsideEqualsOrHashCode(expression)) { return; } + final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false, true); + if (TypeUtils.getType(CommonClassNames.JAVA_LANG_BOOLEAN, expression).equals(expectedType) || + TypeUtils.getType(CommonClassNames.JAVA_LANG_BYTE, expression).equals(expectedType)) { + return; + } registerError(expression, expression, "autoboxing"); } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_instantiation_in_equals_hash_code/ObjectInstantiationInEqualsHashCode.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_instantiation_in_equals_hash_code/ObjectInstantiationInEqualsHashCode.java index 8466b52b3122..e88379f7a530 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_instantiation_in_equals_hash_code/ObjectInstantiationInEqualsHashCode.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_instantiation_in_equals_hash_code/ObjectInstantiationInEqualsHashCode.java @@ -85,4 +85,13 @@ class Y { } } +} +class Autoboxing { + Boolean b = Boolean.FALSE; + + public int hashCode() { + Boolean b1 = true; + Byte b2 = 8; + return !b ? 0 : 1; + } } \ No newline at end of file