diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java index 5d38ffa969cb..28cdf3b653c3 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/FormatDecode.java @@ -188,7 +188,7 @@ class FormatDecode { case 's': // formatted string (general) case 'S': checkFlags(flagBits, LEFT_JUSTIFY | ALTERNATE | PREVIOUS, specifier); - allowed = ALL_VALIDATOR; + allowed = (flagBits & ALTERNATE) != 0 ? new FormattableValidator(specifier) : ALL_VALIDATOR; break; case 'c': // unicode character case 'C': @@ -390,6 +390,18 @@ class FormatDecode { } } + private static class FormattableValidator extends Validator { + + public FormattableValidator(String specifier) { + super(specifier); + } + + @Override + public boolean valid(PsiType type) { + return InheritanceUtil.isInheritor(type, "java.util.Formattable"); + } + } + private static class MultiValidator extends Validator { private final Set validators = new HashSet<>(3); diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/malformed_format_string/MalformedFormatString.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/malformed_format_string/MalformedFormatString.java index 51343c6851ab..dd2591ad2013 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/malformed_format_string/MalformedFormatString.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/malformed_format_string/MalformedFormatString.java @@ -3,6 +3,7 @@ package com.siyeh.igtest.bugs.malformed_format_string; import java.sql.Timestamp; import java.util.Date; import java.util.Locale; +import java.util.Formattable; public class MalformedFormatString { @@ -117,3 +118,15 @@ public class MalformedFormatString { System.out.printf("%o %0); + String.format("%#s", 0.5f); + String.format("%#S", "hello"); + String.format("%#S", new Object().hashCode()); + + // should be OK + String.format("%#s", f); + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/MalformedFormatStringInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/MalformedFormatStringInspectionTest.java index 326b108c6b6c..ccbf25ebc808 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/MalformedFormatStringInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/MalformedFormatStringInspectionTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2017 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.bugs; import com.intellij.codeInspection.InspectionProfileEntry; @@ -22,6 +37,11 @@ public class MalformedFormatStringInspectionTest extends LightInspectionTestCase @Override protected String[] getEnvironmentClasses() { return new String[] { + "package java.util;" + + "public interface Formattable {" + + " void formatTo(Formatter formatter, int flags, int width, int precision);" + + "}", + "package java.time.temporal;\n" + "public interface TemporalAccessor {\n" + " boolean isSupported(TemporalField field);\n" +