1

We just caught a tiny bug that I would have expected Clang-Tidy to catch for us, so please let me know if this isn't expected behavior. We have the following piece of code

float value = 3.4f;   // 

result = function(&a, b, c, value);

Where value in the function definition is defined as a uint8_t:

uint8_t function(float* a, uint8_t b, float c, uint8_t value);

So we placed a float in a uint8_t, which casts it to integers, but somehow clang-tidy didn't find a problem with this. Are we using it wrong or doesn't it detect uint8_t's, just ints?

Thanks in advance!

Imara
  • 37
  • 3
  • 1
    Did you enable `PedanticMode`? – StoryTeller - Unslander Monica Oct 29 '21 at 13:00
  • `-Wfloat-conversion` in `clang++` and `g++` catches that. You could turn on `-Werror=float-conversion` when compiling as an extra safety guard. If you use `clang++` to compile, you could use `-Weverything` and instead turn off those specific warnings that you don't want. `-Wno-c++98-compat` for example. – Ted Lyngmo Oct 29 '21 at 13:08
  • Implicit conversion from `float` to `uint8_t` is definitely fishy, since both sign and value bits could be lost. As for what kind of problems clang-tidy claims to catch, I have no idea. – Lundin Oct 29 '21 at 13:12
  • 1
    Sorry for my c++ comment above - but `-Werror=float-conversion` and `-Weverything` works for C too if you use `clang` or `gcc`. – Ted Lyngmo Oct 29 '21 at 13:17
  • @StoryTeller-UnslanderMonica, no I don't. We actually use gnu99 as the C compiler in MCUxpresso and are using clang-tidy as an additional tool, so I wasn't aware that I should add additional flags in the compile command. I wouldn't necessarily call this a pedantic error though right? It loses sign and value. – Imara Oct 29 '21 at 13:27
  • 1
    That's a clang-tidy option... https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.html – StoryTeller - Unslander Monica Oct 29 '21 at 13:28
  • @TedLyngmo that definitely seems to work! Thanks a lot! – Imara Oct 29 '21 at 13:33
  • @Imara Cool, glad it worked! Cheers! – Ted Lyngmo Oct 29 '21 at 13:34
  • hehehe your're right @chux-ReinstateMonica, I edited the question, off course this was paraphrasing my code a bit, it wasn't actually 10. – Imara Oct 29 '21 at 13:50
  • @StoryTeller-UnslanderMonica ah ok! I misunderstood what you meant. No I didn't, because these values aren't actually rounded up numbers. I edited my example as suggested, so I suppose turning on pedantic mode won't work. – Imara Oct 29 '21 at 13:51
  • 1
    lmara, Note with `float value = 3.4;` you have injected a new [conversion issue](https://stackoverflow.com/q/66631288/2410359). Suggest `float value = 3.4f;` to focus on the key issue of the post. – chux - Reinstate Monica Oct 29 '21 at 14:06
  • 1
    @chux-ReinstateMonica Nice Q&A! – Ted Lyngmo Oct 29 '21 at 14:14
  • You are super right @chux-ReinstateMonica, thanks! – Imara Oct 29 '21 at 15:04

0 Answers0