From 898800322f09587a44c61ed1a2f072e7cf7420e6 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 22 Jul 2020 14:20:18 +0200 Subject: [PATCH] IG: warn on the problematic whitespace, not the next statement (IDEA-246303) GitOrigin-RevId: ebbae6d5e40b852c2312b415fa45261ea5023fa1 --- .../messages/InspectionGadgetsBundle.properties | 2 +- ...ousIndentAfterControlStatementInspection.java | 12 +++++++----- .../SuspiciousIndentAfterControlStatement.java | 16 +++++++++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/resources/messages/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/resources/messages/InspectionGadgetsBundle.properties index fe24a90938bc..64f7ee56f5e8 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/resources/messages/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/resources/messages/InspectionGadgetsBundle.properties @@ -1572,7 +1572,7 @@ explicit.array.to.string.problem.descriptor=Call to '#ref()' on array #loc implicit.array.to.string.method.call.problem.descriptor=Implicit call to 'toString()' on array returned by call to #ref #loc implicit.array.to.string.display.name=Call to 'toString()' on array implicit.array.to.string.quickfix=Wrap with ''{0}'' expression -suspicious.indent.after.control.statement.problem.descriptor=#ref statement has suspicious indentation #loc +suspicious.indent.after.control.statement.problem.descriptor=Suspicious indentation after ''{0}'' statement #loc suspicious.indent.after.control.statement.display.name=Suspicious indentation after control statement without braces unpredictable.big.decimal.constructor.call.display.name=Unpredictable BigDecimal constructor call unpredictable.big.decimal.constructor.call.problem.descriptor=Unpredictable new #ref() call #loc diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/SuspiciousIndentAfterControlStatementInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/SuspiciousIndentAfterControlStatementInspection.java index 8f02c36d2444..60a120266958 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/SuspiciousIndentAfterControlStatementInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/SuspiciousIndentAfterControlStatementInspection.java @@ -18,7 +18,9 @@ public class SuspiciousIndentAfterControlStatementInspection extends BaseInspect @Override @NotNull protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message("suspicious.indent.after.control.statement.problem.descriptor"); + final PsiStatement statement = (PsiStatement)infos[0]; + final PsiElement token = statement.getFirstChild(); + return InspectionGadgetsBundle.message("suspicious.indent.after.control.statement.problem.descriptor", token.getText()); } @Override @@ -97,7 +99,7 @@ public class SuspiciousIndentAfterControlStatementInspection extends BaseInspect final int statementIndent = getIndent(siblingText.substring(statementLineBreak + 1)); final int bodyIndent = getIndent(text.substring(bodyLineBreak + 1)); if (statementIndent == bodyIndent) { - registerStatementError(body); + registerErrorAtOffset(bodyWhiteSpace, bodyLineBreak + 1, bodyIndent, statement); return; } } @@ -125,12 +127,12 @@ public class SuspiciousIndentAfterControlStatementInspection extends BaseInspect final int bodyIndent = getIndent(text.substring(bodyLineBreak + 1)); final int nextIndent = getIndent(nextText.substring(nextLineBreak + 1)); if (lineBreakBeforeBody) { - if (nextIndent == bodyIndent) { - registerStatementError(nextStatement); + if (nextIndent >= bodyIndent) { + registerErrorAtOffset(nextWhiteSpace, nextLineBreak + 1, nextIndent, statement); } } else if (nextIndent > bodyIndent) { - registerStatementError(nextStatement); + registerErrorAtOffset(nextWhiteSpace, nextLineBreak + 1, nextIndent, statement); } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/suspicious_indent_after_control_statement/SuspiciousIndentAfterControlStatement.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/suspicious_indent_after_control_statement/SuspiciousIndentAfterControlStatement.java index e431fa4a35f5..8b224b3cac2f 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/suspicious_indent_after_control_statement/SuspiciousIndentAfterControlStatement.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/suspicious_indent_after_control_statement/SuspiciousIndentAfterControlStatement.java @@ -9,7 +9,7 @@ public class SuspiciousIndentAfterControlStatement { case 1: if (j / 8 == 0) return "x"; - case 0: + case 0: if (j % 2 == 0) return "even"; else @@ -23,9 +23,9 @@ public class SuspiciousIndentAfterControlStatement { void indent(int i) { if (i ==9) System.out.println("foo"); - System.out.println("bar"); + System.out.println("bar"); if (i == 10); - System.out.println("great"); + System.out.println("great"); } @@ -40,7 +40,7 @@ public class SuspiciousIndentAfterControlStatement { void m() { if (true) System.out.println(); - class ; + class ; } class Lol { @@ -88,7 +88,13 @@ public class SuspiciousIndentAfterControlStatement { public void x(int i) { if (i == 3) - \")' statement has suspicious indentation">System.out.println("-->"); + System.out.println("-->"); System.out.println(i); } + + public void y(int i) { + if (i == 42) + System.out.println("answer"); + System.out.println("question"); + } } \ No newline at end of file