Bug 35214 - -Wconversion produces spurious warnings
Summary: -Wconversion produces spurious warnings
Status: RESOLVED DUPLICATE of bug 9072
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2008-02-15 20:44 UTC by Jason Vas Dias
Modified: 2008-02-15 20:50 UTC (History)
11 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Jason Vas Dias 2008-02-15 20:44:33 UTC
Compiling this code with -Wconversion produces a spurious warning -
(I've tried gcc versions 4.2.3, 4.2.2, 3.4.6, 3.4.3, on both Linux
and Solaris, and get the same warning): 

---{ w_conversion_bug.c listing :
1:     extern unsigned short my_htons( unsigned short );
2:     void f(void)
3:     {   unsigned short port = 0x1234, n_port;
4:         n_port = my_htons( port );        
5:     }
---{ compilation listing:
       $ gcc -Wconversion -c w_conversion_bug.c
       w_conversion_bug.c:4: warning: passing argument 1 of 'my_htons' with \   
       different width due to prototype

This also happens for any code that invokes the system htons() with
an unsigned short argument, on modern Linux glibc and Solaris libc systems.

-Wconversion appears to be incorrectly complaining about a nonexistent "conversion" - from "unsigned short" to "unsigned short" ?!?!?

OK, analyzing the documentation, I see -Wconversion is meant to be warning
about :
  "if a prototype causes a type conversion that is different
   from what would happen to the same argument in the absence of a
   prototype" ;
Yes, without a prototype here, the argument to my_htons would have been
an "int", so -Wconversion is warning that the argument is not an int.
But the code is passing the correct parameter type for the function!
And if I make the type of "port" into an "int", precisely the same
-Wconversion warning results, even though this would be a serious
error in the case of trying to use the system htons() with an int argument.
So how can one use -Wconversion to discover potential conversion errors?

These spurious warnings make what should be an extremely useful gcc warning
feature practically unusable and rather useless .

I think -Wconversion should produce a warning when
the type of the parameters passed in do not match the prototype,
necessitating a conversion - that would be really useful! - but
not when exactly the correct type according to the prototype is
passed and no conversion is required.
Please consider fixing this in a future release - thank you!
Comment 1 Andrew Pinski 2008-02-15 20:50:35 UTC
> w_conversion_bug.c:4: warning: passing argument 1 of 'my_htons' with  different width due to prototype

This is the old style -Wconversion warning which got changed for 4.3.0, see PR 9072.

*** This bug has been marked as a duplicate of 9072 ***