Merge pull request #2604 from geoffw0/returnthis

CPP: Exclude template classes from cpp/assignment-does-not-return-this
This commit is contained in:
Dave Bartolomeo
2020-01-08 09:12:22 -07:00
committed by GitHub
4 changed files with 21 additions and 3 deletions

View File

@@ -93,6 +93,10 @@ predicate assignOperatorWithWrongResult(Operator op, string msg) {
from Operator op, string msg
where
assignOperatorWithWrongType(op, msg) or
assignOperatorWithWrongResult(op, msg)
(
assignOperatorWithWrongType(op, msg) or
assignOperatorWithWrongResult(op, msg)
) and
// exclude code in templates which may be incomplete
not op.isFromUninstantiatedTemplate(_)
select op, msg

View File

@@ -201,6 +201,18 @@ struct TemplatedAssignmentBad {
}
};
template<class T>
class Obj3 {
public:
Obj3<T> &subFunc(const Obj3<T> &other) {
return *this;
}
Obj3<T> &operator=(const Obj3<T> &other) {
return subFunc(other); // GOOD (returns *this)
}
};
int main() {
Container c;
c = c;
@@ -211,5 +223,7 @@ int main() {
taGood = 3;
TemplatedAssignmentBad taBad;
taBad = 4;
Obj3<int> obj3_a, obj3_b;
obj3_a = obj3_b;
return 0;
}

View File

@@ -1,5 +1,4 @@
| AV Rule 82.cpp:18:9:18:17 | operator= | Assignment operator in class Bad1 does not return a reference to *this. |
| AV Rule 82.cpp:24:8:24:16 | operator= | Assignment operator in class Bad2 should have return type Bad2&. Otherwise a copy is created at each call. |
| AV Rule 82.cpp:63:29:63:29 | operator= | Assignment operator in class TemplateReturnAssignment<int> does not return a reference to *this. |
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<T> does not return a reference to *this. |
| AV Rule 82.cpp:199:52:199:52 | operator= | Assignment operator in class TemplatedAssignmentBad should have return type TemplatedAssignmentBad&. Otherwise a copy is created at each call. |