Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c8de44

Browse files
author
Dave Bartolomeo
authored
Merge pull request #2604 from geoffw0/returnthis
CPP: Exclude template classes from cpp/assignment-does-not-return-this
2 parents e103527 + b6e1f35 commit 6c8de44

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

change-notes/1.24/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following changes in version 1.24 affect C/C++ analysis in all applications.
2020
| Missing return statement (`cpp/missing-return`) | Fewer false positive results | Functions containing `asm` statements are no longer highlighted by this query. |
2121
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | | This query is no longer run on LGTM. |
2222
| No space for zero terminator (`cpp/no-space-for-terminator`) | Fewer false positive results | This query has been modified to be more conservative when identifying which pointers point to null-terminated strings. This approach produces fewer, more accurate results. |
23+
| Overloaded assignment does not return 'this' (`cpp/assignment-does-not-return-this`) | Fewer false positive results | This query no longer reports incorrect results in template classes. |
2324
| Unsafe array for days of the year (`cpp/leap-year/unsafe-array-for-days-of-the-year`) | | This query is no longer run on LGTM. |
2425

2526
## Changes to libraries

cpp/ql/src/jsf/4.10 Classes/AV Rule 82.ql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ predicate assignOperatorWithWrongResult(Operator op, string msg) {
9393

9494
from Operator op, string msg
9595
where
96-
assignOperatorWithWrongType(op, msg) or
97-
assignOperatorWithWrongResult(op, msg)
96+
(
97+
assignOperatorWithWrongType(op, msg) or
98+
assignOperatorWithWrongResult(op, msg)
99+
) and
100+
// exclude code in templates which may be incomplete
101+
not op.isFromUninstantiatedTemplate(_)
98102
select op, msg

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/AV Rule 82.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ struct TemplatedAssignmentBad {
201201
}
202202
};
203203

204+
template<class T>
205+
class Obj3 {
206+
public:
207+
Obj3<T> &subFunc(const Obj3<T> &other) {
208+
return *this;
209+
}
210+
211+
Obj3<T> &operator=(const Obj3<T> &other) {
212+
return subFunc(other); // GOOD (returns *this)
213+
}
214+
};
215+
204216
int main() {
205217
Container c;
206218
c = c;
@@ -211,5 +223,7 @@ int main() {
211223
taGood = 3;
212224
TemplatedAssignmentBad taBad;
213225
taBad = 4;
226+
Obj3<int> obj3_a, obj3_b;
227+
obj3_a = obj3_b;
214228
return 0;
215229
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
| AV Rule 82.cpp:18:9:18:17 | operator= | Assignment operator in class Bad1 does not return a reference to *this. |
22
| 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. |
33
| AV Rule 82.cpp:63:29:63:29 | operator= | Assignment operator in class TemplateReturnAssignment<int> does not return a reference to *this. |
4-
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<T> does not return a reference to *this. |
54
| 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. |

0 commit comments

Comments
 (0)