From: Louis Brandy Date: Thu, 20 Jun 2013 00:01:38 +0000 (-0700) Subject: fix tautological comparisons in Conv.h X-Git-Tag: v0.22.0~941 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=423eed2d591e7b43c0aeb4e3814ebd5b1a127aa9;p=folly.git 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 --- diff --git a/folly/Traits.h b/folly/Traits.h index f09a9136..e5f6b38c 100644 --- a/folly/Traits.h +++ b/folly/Traits.h @@ -332,7 +332,7 @@ template bool less_than_impl( typename std::enable_if< (rhs <= std::numeric_limits::max() - && rhs >= std::numeric_limits::min()), + && rhs > std::numeric_limits::min()), LHS >::type const lhs ) { @@ -352,7 +352,7 @@ bool less_than_impl( template bool less_than_impl( typename std::enable_if< - (rhs < std::numeric_limits::min()), + (rhs <= std::numeric_limits::min()), LHS >::type const ) { diff --git a/folly/test/TraitsTest.cpp b/folly/test/TraitsTest.cpp index 7a80c6cc..784524e1 100644 --- a/folly/test/TraitsTest.cpp +++ b/folly/test/TraitsTest.cpp @@ -96,6 +96,21 @@ TEST(Traits, is_negative) { 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(0u))); + EXPECT_FALSE((folly::less_than(254u))); + EXPECT_FALSE((folly::less_than(255u))); + EXPECT_TRUE( (folly::less_than(254u))); + + EXPECT_FALSE((folly::greater_than(0u))); + EXPECT_TRUE( (folly::greater_than(254u))); + EXPECT_FALSE((folly::greater_than(255u))); + EXPECT_FALSE((folly::greater_than(254u))); +} + struct CompleteType {}; struct IncompleteType; TEST(Traits, is_complete) {