Skip to content

Commit 6385dd3

Browse files
committed
C++: Exclude operator= in ExprHasNoEffect
1 parent 95e457c commit 6385dd3

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

cpp/ql/src/Likely Bugs/Likely Typos/ExprHasNoEffect.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ where // EQExprs are covered by CompareWhereAssignMeant.ql
9797
not peivc instanceof EQExpr and
9898
// as is operator==
9999
not peivc.(FunctionCall).getTarget().hasName("operator==") and
100+
// An assignment operator may have no side effects in its current
101+
// implementation, but we should not advise callers to rely on this. That
102+
// would break encapsulation.
103+
not peivc.(FunctionCall).getTarget().hasName("operator=") and
100104
not accessInInitOfForStmt(peivc) and
101105
not peivc.isCompilerGenerated() and
102106
not exists(Macro m | peivc = m.getAnInvocation().getAnExpandedElement()) and

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/ExprHasNoEffect/ExprHasNoEffect.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
| test.c:27:9:27:10 | 33 | This expression has no effect. | test.c:27:9:27:10 | 33 | |
2828
| test.cpp:24:3:24:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
2929
| test.cpp:25:3:25:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
30-
| test.cpp:62:5:62:5 | call to operator= | This expression has no effect (because $@ has no external side effects). | test.cpp:47:14:47:22 | operator= | operator= |
31-
| test.cpp:65:5:65:5 | call to operator= | This expression has no effect (because $@ has no external side effects). | test.cpp:55:7:55:7 | operator= | operator= |
32-
| test.cpp:91:5:91:19 | call to operator= | This expression has no effect (because $@ has no external side effects). | test.cpp:77:9:77:17 | operator= | operator= |
3330
| volatile.c:9:5:9:5 | c | This expression has no effect. | volatile.c:9:5:9:5 | c | |
3431
| volatile.c:12:5:12:9 | access to array | This expression has no effect. | volatile.c:12:5:12:9 | access to array | |
3532
| volatile.c:16:5:16:7 | * ... | This expression has no effect. | volatile.c:16:5:16:7 | * ... | |

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/ExprHasNoEffect/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct Derived : Base {
8888
}
8989

9090
// In case base class has data, now or in the future, copy that first.
91-
Base::operator=(rhs); // GOOD [FALSE POSITIVE]
91+
Base::operator=(rhs); // GOOD
9292

9393
this->m_x = rhs.m_x;
9494
return *this;

0 commit comments

Comments
 (0)