Skip to content

Parameter Assignment Checkstyle C++

Description

Since Checkstyle 3.2

Checks that classes which define a covariant method also override method .
Covariant - method that is similar to , but with a covariant parameter type (any subtype of Object).
Notice: the enums are also checked, even though they cannot override . The reason is to point out that implementing in enums is considered an awful practice: it may cause having two different enum values that are equal using covariant enum method, and not equal when compared normally.

Inspired by Finding Bugs is Easy, chapter '2.3.1 Bad Covariant Definition of Equals (Eq)':

Java classes may override the method to define a predicate for object equality. This method is used by many of the Java runtime library classes; for example, to implement generic containers.

Programmers sometimes mistakenly use the type of their class as the type of the parameter to :

public boolean equals(Foo obj) {...}

This covariant version of does not override the version in the class, and it may lead to unexpected behavior at runtime, especially if the class is used with one of the standard collection classes which expect that the standard method is overridden.

This kind of bug is not obvious because it looks correct, and in circumstances where the class is accessed through the references of the class type (rather than a supertype), it will work correctly. However, the first time it is used in a container, the behavior might be mysterious. For these reasons, this type of bug can elude testing and code inspections.

Examples

To configure the check:

<module name="CovariantEquals"/>

For example:

class Test { public boolean equals(Test i) { // violation return false; } }

The same class without violations:

class Test { public boolean equals(Test i) { // no violation return false; } public boolean equals(Object i) { return false; } }

Error Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Description

Since Checkstyle 5.8

The Check validate abbreviations(consecutive capital letters) length in identifier name, it also allows to enforce camel case naming. Please read more at Google Style Guide to get to know how to avoid long abbreviations in names.

allowedAbbreviationLength specifies how many consecutive capital letters are allowed in the identifier. A value of 3 indicates that up to 4 consecutive capital letters are allowed, one after the other, before a violation is printed. The identifier 'MyTEST' would be allowed, but 'MyTESTS' would not be. A value of 0 indicates that only 1 consecutive capital letter is allowed. This is what should be used to enforce strict camel casing. The identifier 'MyTest' would be allowed, but 'MyTEst' would not be.

Properties

namedescriptiontypedefault valuesince
allowedAbbreviationLengthindicates on the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ).Integer35.8
allowedAbbreviationslist of abbreviations that must be skipped for checking. Abbreviations should be separated by comma, no spaces are allowed.String Set{}5.8
ignoreFinalallow to skip variables with final modifier.Booleantrue5.8
ignoreStaticallow to skip variables with static modifier.Booleantrue5.8
ignoreOverriddenMethodsAllows to ignore methods tagged with @Override annotation (that usually mean inherited name).Booleantrue5.8
tokenstokens to check subset of tokens CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, ENUM_CONSTANT_DEF. CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF. 5.8

Examples

Default configuration

<module name="AbbreviationAsWordInName"/>

To configure to check variables and classes identifiers, do not ignore variables with static modifier and allow no abbreviations (enforce camel case phrase) and allow no abbreviations to use (camel case phrase) and allow XML and URL abbreviations.

<module name="AbbreviationAsWordInName"> <property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/> <property name="ignoreStatic" value="false"/> <property name="allowedAbbreviationLength" value="1"/> <property name="allowedAbbreviations" value="XML,URL"/> </module>

Error Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.naming