LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 32246 - [misc-misplaced-widening-cast] is wrong when passing arguments to functions results in an implicit cast
Summary: [misc-misplaced-widening-cast] is wrong when passing arguments to functions r...
Status: RESOLVED WORKSFORME
Alias: None
Product: clang-tools-extra
Classification: Unclassified
Component: clang-tidy (show other bugs)
Version: unspecified
Hardware: PC All
: P enhancement
Assignee: Daniel Marjamäki
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-03-12 08:09 PDT by Gonzalo BG
Modified: 2017-08-04 01:09 PDT (History)
3 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gonzalo BG 2017-03-12 08:09:33 PDT
Consider this code:

auto foo(long);

int i = 0;
foo(i + 1); 
    ^
 warning: either cast from 'int' to 'long'  is ineffective, or there is loss of precision before the conversion [misc-misplaced-widening-cast]

The warning suggest that I should cast i to long, then doing the arithmetic, and then passing it to foo, _OR_ that I should remove the cast from i to long by changing the function signature.

> If there is no loss of precision then the cast can be removed 

But this is false at least for all the functions outside your control (e.g. functions declared in external or system headers). You cannot change those, hence, you cannot remove the cast.

I don't know what should be done here, but a first step could be to offer more fine tuned control about which kind of implicit cases can be disabled. I would like to disable the case in which the implicit conversion happens when calling a function (but maybe others want to keep it).
Comment 1 Alexander Kornienko 2017-03-16 07:47:42 PDT
I've noticed the same issue when trying the check on our code.

Assigning to the author of the check.
Comment 2 Daniel Marjamäki 2017-08-04 01:09:19 PDT
I did not intend to show any warnings for implicit casts. I see no reason at all to warn here in this case. Nor other similar cases.

Gabor added checking for implicit casts here:
http://reviews.llvm.org/D17987

And since april that checking is off by default:
https://reviews.llvm.org/D32164

Because "Users reported some false positives using this check".

I cant reproduce with latest revision unless I manually enable the CheckImplicitCasts option:
clang-tidy -checks=* -config="{CheckOptions: [{key: misc-misplaced-widening-cast.CheckImplicitCasts, value: 1}]}" 32246.c --

I close this as worksforme. Feel free to reopen if you still see problems.