Constant return type on member¶
ID: cpp/member-const-no-effectKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - maintainability - readability - language-featuresQuery suites: - cpp-security-and-quality.qls
Click to see the query in the CodeQL repository
This rule finds member functions with a superfluousconst qualifier on their return type. This might be caused by confusion about how to declare aconst method. In particular, when C++11 trailing return types are used, it becomes much easier to mis-declare aconst method by puttingconst on the wrong side of->.
Recommendation¶
The superfluousconst qualifier should be removed, as it serves no purpose. If the return type contains embedded qualifiers, then care should be taken to remove only the superfluous one.
Alternatively, if the intention was to have aconst method, then the qualifier should be moved to immediately after the argument list.
Example¶
structS{intval;// The const has no effect here.autogetValIncorrect()->constint{returnval;}// Whereas here it does make a semantic difference.autogetValCorrect()const->int{returnval;}};