On Wed, Dec 30, 2015 at 12:30 PM, Richard Smith <rich...@metafoo.co.uk> wrote: > On Dec 30, 2015 6:34 AM, "Aaron Ballman" <aaron.ball...@gmail.com> wrote: >> >> aaron.ballman closed this revision. >> aaron.ballman added a comment. >> >> Thanks! I've commit in r256643. >> >> >> ================ >> Comment at: test/SemaCXX/warn-literal-conversion.cpp:49-50 >> @@ +48,4 @@ >> + // values. >> + bool b3 = 0.0f; >> + bool b4 = 0.0; >> +} >> ---------------- >> rsmith wrote: >> > What about >> > >> > bool b5 = 1.0; >> > bool b6 = 2.0; >> > >> > ? Arguably any `float` -> `bool` conversion changes the value (because >> > `true` and `false` are not values of type `float`), so it wouldn't be >> > completely unreasonable to warn even if the literal is `0.0`. >> Except those conversions won't cause confusion to the user, so I'm not >> certain what we gain by diagnosing. Given that some mental models expect >> 0.99 to convert to 0, which converts to false (because bool is an integral >> type, so it "must" do the usual integral truncation dance), it makes sense >> to tell the user "no no no, that converts to true." I'm less convinced about >> the utility of warning on things like `bool b = 1.99f' where it changes the >> value from 1.99 to true. Perhaps this should be changed to only diagnose >> when converting through an integer would result in a different value that >> converting through the float? > > Let me ask a slightly different question: when would it ever be reasonable > and intentional to initialize a bool from a floating-point *literal*? For > integers, people use things like > > int n = 1e6; > > which is why we don't complain if the literal is exactly representable in > the integer type. I can't imagine any similar cases for bool.
I see what you're getting at now, and I agree that it wouldn't be particularly reasonable. However, I think that there are two things to warn about: initializing from a literal in general, and initializing from a literal where the resulting Boolean value may be surprising. While we're at it: is there ever a time where it's reasonable to initialize a bool from a literal of type other than an integer or bool? For instance: bool b1 = L'1'; bool b2 = '1'; bool b3 = 1.0f; bool b4 = nullptr; // -Wnull-conversion catches this I think it makes sense for all of these to diagnose under -Wliteral-conversion, but drop the "changes value" clause. bool b5 = 0.01; bool b6 = '0'; bool b7 = L'0'; I think it makes sense for these to diagnose under -Wliteral-conversion, but with the changes value clause. ~Aaron > >> >> http://reviews.llvm.org/D15814 >> >> >> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits