Skip to content

C++: Fix FP in cpp/comparison-of-identical-expressions #7395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ private predicate cancelingSubExprs(ComparisonOperation cmp, VariableAccess a1,
exists(Variable v |
exists(float m | m < 0 and cmpLinearSubVariable(cmp, v, a1, m)) and
exists(float m | m > 0 and cmpLinearSubVariable(cmp, v, a2, m))
)
) and
not any(ClassTemplateInstantiation inst).getATemplateArgument() = cmp.getParent*()
}

from ComparisonOperation cmp, VariableAccess a1, VariableAccess a2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ predicate pointlessSelfComparison(ComparisonOperation cmp) {
not exists(lhs.getQualifier()) and // Avoid structure fields
not exists(rhs.getQualifier()) and // Avoid structure fields
not convertedExprMightOverflow(lhs) and
not convertedExprMightOverflow(rhs)
not convertedExprMightOverflow(rhs) and
// Don't warn if the comparison is part of a template argument.
not any(ClassTemplateInstantiation inst).getATemplateArgument() = cmp.getParent*()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ bool compareValues() {
bool callCompareValues() {
return compareValues<C1, C2> || compareValues<C1, C1>();
}

template <bool C, typename T = void>
struct enable_if {};

template <typename T>
struct enable_if<true, T> { typedef T type; };

template<typename T1, typename T2>
typename enable_if<T1::value <= T2::value, bool>::type constant_comparison() {
return true;
}

struct Value0 {
const static int value = 0;
};

void instantiation_with_pointless_comparison() {
constant_comparison<Value0, Value0>(); // GOOD
}