public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/110047] New: RFE: Add a warning for use of bare "unsigned" (possibly under -Wimplicit-int?)
@ 2023-05-31  5:54 egallager at gcc dot gnu.org
  2023-05-31  6:54 ` [Bug c/110047] " rguenth at gcc dot gnu.org
  2023-06-01  1:11 ` egallager at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: egallager at gcc dot gnu.org @ 2023-05-31  5:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110047

            Bug ID: 110047
           Summary: RFE: Add a warning for use of bare "unsigned"
                    (possibly under -Wimplicit-int?)
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: enhancement
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: egallager at gcc dot gnu.org
            Blocks: 87403
  Target Milestone: ---

When I was first learning C, one thing that confused me was how you can just
use plain "unsigned" as a type, without specifying the length (long, short,
int, etc.). Thus, I thought that casting to unsigned would just change the sign
like a call to abs(), without realizing that there was an implicit "int"
involved. I made a testcase:

$ cat bare_unsigned.c
#include <limits.h>

unsigned var; /* debatable */

unsigned long foo(void)
{
        long variable = LONG_MAX;
        unsigned long uvariable = (unsigned)variable; /* warn here */
        return uvariable;
}
$

The one where I added the "debatable" comment is debatable because I actually
see a lot of declarations in that form pretty often, and it's probably not very
harmful in that case, but the case with the cast, where it says "warn here", is
probably more deserving of a warning, as there's a change of size involved. It
might make sense to include this under -Wimplicit-int, or maybe create a new
warning -Wbare-unsigned for it?


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87403
[Bug 87403] [Meta-bug] Issues that suggest a new warning

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/110047] RFE: Add a warning for use of bare "unsigned" (possibly under -Wimplicit-int?)
  2023-05-31  5:54 [Bug c/110047] New: RFE: Add a warning for use of bare "unsigned" (possibly under -Wimplicit-int?) egallager at gcc dot gnu.org
@ 2023-05-31  6:54 ` rguenth at gcc dot gnu.org
  2023-06-01  1:11 ` egallager at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-31  6:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110047

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Maybe just diagnose at the point of conversions that are not just sign
conversions but truncations/extensions?

Note even then this will have a high rate of false positives (I'm myself
always short-cutting 'unsigned int' to 'unsigned' ...) so it's more of
a coding-style diagnostic where then warning for all plain 'unsigned'
might be appropriate as well.

So, maybe split it even.  -Wconversion-bare-unsigned and -Wbare-unsigned?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/110047] RFE: Add a warning for use of bare "unsigned" (possibly under -Wimplicit-int?)
  2023-05-31  5:54 [Bug c/110047] New: RFE: Add a warning for use of bare "unsigned" (possibly under -Wimplicit-int?) egallager at gcc dot gnu.org
  2023-05-31  6:54 ` [Bug c/110047] " rguenth at gcc dot gnu.org
@ 2023-06-01  1:11 ` egallager at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: egallager at gcc dot gnu.org @ 2023-06-01  1:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110047

--- Comment #2 from Eric Gallager <egallager at gcc dot gnu.org> ---
Oh, one other thing I'd note here is that gcc/README.portability contains a
part about this, too; I'm copying and pasting it here:

Implicit int
------------

In C, the 'int' keyword can often be omitted from type declarations.
For instance, you can write

  unsigned variable;

as shorthand for

  unsigned int variable;

There are several places where this can cause trouble.  First, suppose
'variable' is a long; then you might think

  (unsigned) variable

would convert it to unsigned long.  It does not.  It converts to
unsigned int.  This mostly causes problems on 64-bit platforms, where
long and int are not the same size.

Ref:
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/README.Portability;h=af6904728833193ba57e74ec5bdd4070992efe42;hb=HEAD#l93

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-01  1:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-31  5:54 [Bug c/110047] New: RFE: Add a warning for use of bare "unsigned" (possibly under -Wimplicit-int?) egallager at gcc dot gnu.org
2023-05-31  6:54 ` [Bug c/110047] " rguenth at gcc dot gnu.org
2023-06-01  1:11 ` egallager at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).