diff --git a/README.md b/README.md index 3c6d2b8..e4c1691 100644 --- a/README.md +++ b/README.md @@ -91,11 +91,13 @@ Rule|Level|Source|Enabled|Suppressible [AvoidConditionInversion](#avoidconditioninversion)|complexity|sevntu|| [AvoidConstantAsFirstOperandInCondition](#avoidconstantasfirstoperandincondition)|tweaks|sevntu|Yes| [AvoidDefaultSerializableInInnerClasses](#avoiddefaultserializableininnerclasses)|tweaks|sevntu|Yes| +[AvoidDoubleBraceInitialization](#avoiddoublebraceinitialization)|tweaks|checkstyle|Yes| [AvoidEscapedUnicodeCharacters](#avoidescapedunicodecharacters)|tweaks|checkstyle|Yes| [AvoidHidingCauseException](#avoidhidingcauseexception)|tweaks|sevntu|Yes| [AvoidInlineConditionals](#avoidinlineconditionals)|complexity|checkstyle|Yes| [AvoidModifiersForTypes](#avoidmodifiersfortypes)|unspecified|sevntu|| [AvoidNestedBlocks](#avoidnestedblocks)|complexity|checkstyle|Yes| +[AvoidNoArgumentSuperConstructorCall](#avoidnoargumentsuperconstructorcall)|tweaks|checkstyle|Yes| [AvoidNotShortCircuitOperatorsForBoolean](#avoidnotshortcircuitoperatorsforboolean)|tweaks|sevntu|Yes| [AvoidStarImport](#avoidstarimport)|layout|checkstyle|| [AvoidStaticImport](#avoidstaticimport)|complexity|checkstyle|| @@ -164,8 +166,8 @@ Rule|Level|Source|Enabled|Suppressible [InterfaceMemberImpliedModifier](#interfacememberimpliedmodifier)|tweaks|checkstyle|| [InterfaceTypeParameterName](#interfacetypeparametername)|naming|checkstyle|Yes| [JavadocMethod](#javadocmethod)|javadoc|checkstyle|| -[JavadocMissingLeadingAsterisk](#javadocmissingleadingasterisk)|layout|checkstyle|Yes| -[JavadocMissingWhitespaceAfterAsterisk](#javadocmissingwhitespaceafterasterisk)|layout|checkstyle|Yes| +[JavadocMissingLeadingAsterisk](#javadocmissingleadingasterisk)|javadoc|checkstyle|Yes| +[JavadocMissingWhitespaceAfterAsterisk](#javadocmissingwhitespaceafterasterisk)|javadoc|checkstyle|Yes| [JavadocPackage](#javadocpackage)|javadoc|checkstyle|Yes| [JavadocParagraph](#javadocparagraph)|javadoc|checkstyle|Yes| [JavadocStyle](#javadocstyle)|javadoc|checkstyle|Yes| @@ -381,6 +383,38 @@ Javadoc `@` clauses must be in the order: * @deprecated ... */ ```` +#### [AvoidDoubleBraceInitialization](https://checkstyle.sourceforge.io/config_coding.html#AvoidDoubleBraceInitialization) + +Detects double brace initialization. + +Rationale: Double brace initialization (set of Instance Initializers in class +body) may look cool, but it is considered as anti-pattern and should be avoided. +This is also can lead to a hard-to-detect memory leak, if the anonymous class +instance is returned outside and other object(s) hold reference to it. Created +anonymous class is not static, it holds an implicit reference to the outer class +instance. See this +[blog post](https://blog.jooq.org/2014/12/08/dont-be-clever-the-double-curly-braces-anti-pattern/) +and +[article](https://www.baeldung.com/java-double-brace-initialization) +for more details. Check ignores any comments and semicolons in class body. + +Invalid: +````java +class MyClass { + List list1 = new ArrayList<>() { // violation + { + add(1); + } + }; + List list2 = new ArrayList<>() { // violation + ; + // comments and semicolons are ignored + { + add("foo"); + } + }; +} +```` #### [AvoidEscapedUnicodeCharacters](http://checkstyle.sourceforge.net/config_misc.html#AvoidEscapedUnicodeCharacters) Prevents use of obscure escape codes (e.g. `\u221e`). However, non-printable/control characters are still permitted. @@ -416,6 +450,34 @@ Invalid: // ... } ```` +#### [AvoidNoArgumentSuperConstructorCall](https://checkstyle.sourceforge.io/config_coding.html#AvoidNoArgumentSuperConstructorCall) + +Checks if call to superclass constructor without arguments is present. Such invocation is redundant +because constructor body implicitly begins with a superclass constructor invocation super(); +See +[specification](https://docs.oracle.com/javase/specs/jls/se16/html/jls-8.html#jls-8.8.7) +for detailed information. + +Valid: +````java +class MyClass extends SomeOtherClass { + MyClass(int arg) { + super(arg); // OK, call with argument have to be explicit + } + MyClass(long arg) { + // OK, call is implicit + } +} +```` + +Invalid: +````java +class MyClass extends SomeOtherClass { + MyClass() { + super(); // violation + } +} +```` #### [BooleanExpressionComplexity](http://checkstyle.sourceforge.net/config_metrics.html#BooleanExpressionComplexity) Restrict the number of number of &&, ||, &, | and ^ in an expression to 2. diff --git a/builder/src/main/resources/application.yml b/builder/src/main/resources/application.yml index c37c3fb..2c8efa1 100644 --- a/builder/src/main/resources/application.yml +++ b/builder/src/main/resources/application.yml @@ -1548,7 +1548,7 @@ rules: - name: JavadocMissingLeadingAsterisk parent: TREEWALKER - level: LAYOUT + level: JAVADOC enabled: true source: CHECKSTYLE uri: https://checkstyle.sourceforge.io/config_javadoc.html#JavadocMissingLeadingAsterisk @@ -1569,7 +1569,7 @@ rules: - name: JavadocMissingWhitespaceAfterAsterisk parent: TREEWALKER - level: LAYOUT + level: JAVADOC enabled: true source: CHECKSTYLE uri: https://checkstyle.sourceforge.io/config_javadoc.html#JavadocMissingWhitespaceAfterAsterisk @@ -1583,7 +1583,14 @@ rules: - name: AvoidDoubleBraceInitialization parent: TREEWALKER - level: LAYOUT + level: TWEAKS enabled: true source: CHECKSTYLE uri: https://checkstyle.sourceforge.io/config_coding.html#AvoidDoubleBraceInitialization + - + name: AvoidNoArgumentSuperConstructorCall + parent: TREEWALKER + level: TWEAKS + enabled: true + source: CHECKSTYLE + uri: https://checkstyle.sourceforge.io/config_coding.html#AvoidNoArgumentSuperConstructorCall diff --git a/builder/src/main/resources/rules/AvoidNoArgumentSuperConstructorCall.md b/builder/src/main/resources/rules/AvoidNoArgumentSuperConstructorCall.md new file mode 100644 index 0000000..3162755 --- /dev/null +++ b/builder/src/main/resources/rules/AvoidNoArgumentSuperConstructorCall.md @@ -0,0 +1,27 @@ + +Checks if call to superclass constructor without arguments is present. Such invocation is redundant +because constructor body implicitly begins with a superclass constructor invocation super(); +See +[specification](https://docs.oracle.com/javase/specs/jls/se16/html/jls-8.html#jls-8.8.7) +for detailed information. + +Valid: +````java +class MyClass extends SomeOtherClass { + MyClass(int arg) { + super(arg); // OK, call with argument have to be explicit + } + MyClass(long arg) { + // OK, call is implicit + } +} +```` + +Invalid: +````java +class MyClass extends SomeOtherClass { + MyClass() { + super(); // violation + } +} +```` diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml index effd1e8..06b7f26 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-1-layout.xml @@ -53,9 +53,7 @@ - - diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml index fc62f88..1710366 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-2-naming.xml @@ -80,9 +80,7 @@ - - diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml index 2dcdf67..4d8c473 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml @@ -146,6 +146,8 @@ + + diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml index 0ed63b5..eff56e4 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml @@ -193,6 +193,8 @@ + +