From ad2647fcc827c1ccf28713cebb65d6beec1f6aa3 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 6 Feb 2014 15:03:21 +0100 Subject: [PATCH] don't highlight the entire expression and add test --- .../siyeh/InspectionGadgetsBundle.properties | 2 +- .../ObjectAllocationInLoopInspection.java | 2 +- .../ObjectAllocationInLoop.java | 10 ++++++ .../ObjectAllocationInLoopInspectionTest.java | 31 +++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_allocation_in_loop/ObjectAllocationInLoop.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/ObjectAllocationInLoopInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 6234787ce22d..9dd97fa42ddb 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1037,7 +1037,7 @@ non.synchronized.method.overrides.synchronized.method.problem.descriptor=Unsynch public.field.accessed.in.synchronized.context.problem.descriptor=Non-private field #ref accessed in synchronized context #loc field.accessed.synchronized.and.unsynchronized.problem.descriptor=Field #ref is accessed in both synchronized and unsynchronized contexts #loc extended.for.statement.problem.descriptor=Extended #ref statement #loc -object.allocation.in.loop.problem.descriptor=Object allocation #ref in loop #loc +object.allocation.in.loop.problem.descriptor=Object allocation new #ref() in loop #loc instantiating.object.to.get.class.object.problem.descriptor=Instantiating object to get Class object #loc field.may.be.static.problem.descriptor=Field #ref may be 'static' #loc method.may.be.static.problem.descriptor=Method #ref() may be 'static' #loc diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ObjectAllocationInLoopInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ObjectAllocationInLoopInspection.java index 15919d42b791..e0024e078d04 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ObjectAllocationInLoopInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ObjectAllocationInLoopInspection.java @@ -71,7 +71,7 @@ public class ObjectAllocationInLoopInspection extends BaseInspection { if (isAllocatedOnlyOnce(expression)) { return; } - registerError(expression); + registerNewExpressionError(expression); } private static boolean isAllocatedOnlyOnce( diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_allocation_in_loop/ObjectAllocationInLoop.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_allocation_in_loop/ObjectAllocationInLoop.java new file mode 100644 index 000000000000..58581dfe2e3a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/object_allocation_in_loop/ObjectAllocationInLoop.java @@ -0,0 +1,10 @@ +package com.siyeh.igtest.performance.object_allocation_in_loop; + +class ObjectAllocationInLoop { + + void m() { + while (true) { + new Object(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/ObjectAllocationInLoopInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/ObjectAllocationInLoopInspectionTest.java new file mode 100644 index 000000000000..0fcd297f6771 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/ObjectAllocationInLoopInspectionTest.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.performance; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class ObjectAllocationInLoopInspectionTest extends LightInspectionTestCase { + @Override + protected InspectionProfileEntry getInspection() { + return new ObjectAllocationInLoopInspection(); + } + + public void testObjectAllocationInLoop() { doTest(); } +}