Skip to content

Latest commit

 

History

History
42 lines (32 loc) · 1.03 KB

File metadata and controls

42 lines (32 loc) · 1.03 KB

assignmentInCondition

Message: Suspicious assignment in condition. Condition 't=s' is always true.
Category: Correctness
Severity: Style
Language: C/C++

Description

A container or iterator is assigned (=) directly inside a condition, where == was probably meant - as written, the condition is always true.

Motivation

if (t = s) assigns s to t and then tests the (always-true, for a container/iterator) result of that assignment - almost certainly a typo for if (t == s). Because it's valid, compiling code, this is easy to miss in review.

How to fix

Before:

#include <string>
void f(const std::string& s) {
    std::string t;
    if (t = s) {} // <- always true, did you mean '=='?
}

After:

#include <string>
void f(const std::string& s) {
    std::string t = s;
    if (!t.empty()) {}
}

Related checkers

  • clarifyCondition.md - a related but distinct mistake: an assignment or bitwise operator next to a comparison with ambiguous precedence.