[PATCH] D53488: [clang-tidy] Improving narrowing conversions

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 14 05:31:20 PST 2018


aaron.ballman added inline comments.


================
Comment at: test/clang-tidy/cppcoreguidelines-narrowing-conversions.cpp:79-81
+  // TODO: Provide an automatic fix if the number is exactly representable in the destination type.
+  f += 2.0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: narrowing conversion from constant 'double' to 'float' [cppcoreguidelines-narrowing-conversions]
----------------
This is not a narrowing conversion -- there should be no diagnostic here. See [dcl.init.list]p7.


================
Comment at: test/clang-tidy/cppcoreguidelines-narrowing-conversions.cpp:99
+void narrow_integer_to_floating() {
+  {
+    long long ll; // 64 bits
----------------
Missing tests involving literals, which are not narrowing conversions. e.g., `float f = 1ULL;` should not diagnose.


================
Comment at: test/clang-tidy/cppcoreguidelines-narrowing-conversions.cpp:174
+  c = uc;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'unsigned char' to 'char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
+  c = us;
----------------
This is only true on architectures where `char` is defined to be `signed char` rather than `unsigned char`.


================
Comment at: test/clang-tidy/cppcoreguidelines-narrowing-conversions.cpp:188
+  i = l;
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'long' to 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
+  i = ll;
----------------
This is only narrowing if `sizeof(int) < sizeof(long)` which is not the case on all architectures.


================
Comment at: test/clang-tidy/cppcoreguidelines-narrowing-conversions.cpp:211
+  ll = ul;
+  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned long' to 'long long' is implementation-defined [cppcoreguidelines-narrowing-conversions]
+  ll = ull;
----------------
Whatttt? How does one narrow from an unsigned 32-bit number to a signed 64-bit number?


================
Comment at: test/clang-tidy/cppcoreguidelines-narrowing-conversions.cpp:228
+void narrow_constant_to_signed_integer_is_not_ok() {
+  char c1 = -128;
+  char c2 = 127;
----------------
This may be narrowing on an implementation that defines `char` to be `unsigned`.


================
Comment at: test/clang-tidy/cppcoreguidelines-narrowing-conversions.cpp:232
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: narrowing conversion from constant value -129 (0xFFFFFF7F) of type 'int' to 'char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
+  char c4 = 128;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: narrowing conversion from constant value 128 (0x00000080) of type 'int' to 'char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
----------------
This may be fine on implementations that define `char` to be `unsigned`.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D53488





More information about the cfe-commits mailing list