Commit 423eed2d authored by Louis Brandy's avatar Louis Brandy Committed by Jordan DeLong

fix tautological comparisons in Conv.h

Summary:
We have an off-by-one in our enable_if/sfinae logic here. We do not want to actually do the comparison in `less_than` when the rhs is exactly the minimum possible lhs. This results in a tautological comparison.

I added a unit test for these traits that test all the various sfinae cases.

Test Plan: See the clang tautological warnings go away. Under gcc, rerun all tests. All pass.

Reviewed By: marcelo.juchem@fb.com

FB internal diff: D856869
parent 8949d553
...@@ -332,7 +332,7 @@ template <typename RHS, RHS rhs, typename LHS> ...@@ -332,7 +332,7 @@ template <typename RHS, RHS rhs, typename LHS>
bool less_than_impl( bool less_than_impl(
typename std::enable_if< typename std::enable_if<
(rhs <= std::numeric_limits<LHS>::max() (rhs <= std::numeric_limits<LHS>::max()
&& rhs >= std::numeric_limits<LHS>::min()), && rhs > std::numeric_limits<LHS>::min()),
LHS LHS
>::type const lhs >::type const lhs
) { ) {
...@@ -352,7 +352,7 @@ bool less_than_impl( ...@@ -352,7 +352,7 @@ bool less_than_impl(
template <typename RHS, RHS rhs, typename LHS> template <typename RHS, RHS rhs, typename LHS>
bool less_than_impl( bool less_than_impl(
typename std::enable_if< typename std::enable_if<
(rhs < std::numeric_limits<LHS>::min()), (rhs <= std::numeric_limits<LHS>::min()),
LHS LHS
>::type const >::type const
) { ) {
......
...@@ -96,6 +96,21 @@ TEST(Traits, is_negative) { ...@@ -96,6 +96,21 @@ TEST(Traits, is_negative) {
EXPECT_FALSE(folly::is_non_positive(1u)); EXPECT_FALSE(folly::is_non_positive(1u));
} }
TEST(Traits, relational) {
// We test, especially, the edge cases to make sure we don't
// trip -Wtautological-comparisons
EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(0u)));
EXPECT_FALSE((folly::less_than<uint8_t, 0u, uint8_t>(254u)));
EXPECT_FALSE((folly::less_than<uint8_t, 255u, uint8_t>(255u)));
EXPECT_TRUE( (folly::less_than<uint8_t, 255u, uint8_t>(254u)));
EXPECT_FALSE((folly::greater_than<uint8_t, 0u, uint8_t>(0u)));
EXPECT_TRUE( (folly::greater_than<uint8_t, 0u, uint8_t>(254u)));
EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(255u)));
EXPECT_FALSE((folly::greater_than<uint8_t, 255u, uint8_t>(254u)));
}
struct CompleteType {}; struct CompleteType {};
struct IncompleteType; struct IncompleteType;
TEST(Traits, is_complete) { TEST(Traits, is_complete) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment