public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Uecker <uecker@tugraz.at>
To: gcc-patches@gcc.gnu.org
Cc: Joseph Myers <joseph@codesourcery.com>
Subject: c: Support for -Wuseless-cast [RR84510]
Date: Thu, 10 Aug 2023 13:39:45 +0200	[thread overview]
Message-ID: <573a12823f25ecf649457f32018f2f53a2a8b3a2.camel@tugraz.at> (raw)



This patch adds the missing support for -Wuseless-cast
to the C FE as requested by some users. It found about 
50 useless casts in one of my projects without false 
positives.

(I also implemented a detection for various
unneeded pointer casts in convert_for_assignment
such as unneeded casts from / to void or casts
followed by an implicit conversion to the original
type, but I did not figure out how to reliably 
identify casts there... But this would be a potential
future enhancement.)


Regression tested on bootstrapped on x86_64-pc-linux-gnu.



c: Support for -Wuseless-cast [RR84510]
    
Add support for Wuseless-cast C (and ObjC).
    
PR c/84510
    
gcc/c/:
	* c-typeck.cc (build_c_cast): Add warning.
    
gcc/doc/:
	* invoke.texi: Update.
    
gcc/testsuite/:
	* Wuseless-cast.c: New test.

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 0ed87fcc7be..c7b567ba7ab 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1490,7 +1490,7 @@ C++ ObjC++ Var(warn_zero_as_null_pointer_constant) Warning
 Warn when a literal '0' is used as null pointer.
 
 Wuseless-cast
-C++ ObjC++ Var(warn_useless_cast) Warning
+C ObjC C++ ObjC++ Var(warn_useless_cast) Warning
 Warn about useless casts.
 
 Wsubobject-linkage
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 7cf411155c6..6f2fff51683 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -6062,9 +6062,13 @@ build_c_cast (location_t loc, tree type, tree expr)
 
   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
     {
-      if (RECORD_OR_UNION_TYPE_P (type))
-	pedwarn (loc, OPT_Wpedantic,
-		 "ISO C forbids casting nonscalar to the same type");
+      if (RECORD_OR_UNION_TYPE_P (type)
+	  && pedwarn (loc, OPT_Wpedantic,
+		      "ISO C forbids casting nonscalar to the same type"))
+	      ;
+      else if (warn_useless_cast)
+	warning_at (loc, OPT_Wuseless_cast,
+		    "useless cast to type %qT", type);
 
       /* Convert to remove any qualifiers from VALUE's type.  */
       value = convert (type, value);
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 674f956f4b8..75ca72f3190 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -4772,7 +4772,7 @@ pointers after reallocation.
 
 @opindex Wuseless-cast
 @opindex Wno-useless-cast
-@item -Wuseless-cast @r{(C++ and Objective-C++ only)}
+@item -Wuseless-cast @r{(C, Objective-C, C++ and Objective-C++ only)}
 Warn when an expression is cast to its own type.  This warning does not
 occur when a class object is converted to a non-reference type as that
 is a way to create a temporary:
diff --git a/gcc/testsuite/gcc.dg/Wuseless-cast.c b/gcc/testsuite/gcc.dg/Wuseless-cast.c
new file mode 100644
index 00000000000..86e87584b87
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wuseless-cast.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+/* { dg-options "-Wuseless-cast" } */
+
+void foo(void)
+{	
+	// casts to the same type
+	int i = 0;
+	const int ic = 0;
+	struct foo { int x; } x = { 0 };
+	int q[3];
+	(int)ic;		/* { dg-warning "useless cast" } */
+	(int)i;			/* { dg-warning "useless cast" } */
+	(const int)ic;		/* { dg-warning "useless cast" } */
+	(const int)i;		/* { dg-warning "useless cast" } */
+	(struct foo)x;		/* { dg-warning "useless cast" } */
+	(int(*)[3])&q;		/* { dg-warning "useless cast" } */
+	(_Atomic(int))i;	/* { dg-warning "useless cast" } */
+
+	// not the same
+	int n = 3;
+	(int(*)[n])&q;		// no warning
+	int j = (int)0UL;
+	enum X { A = 1 } xx = { A };
+	enum Y { B = 1 } yy = (enum Y)xx;
+}
+



             reply	other threads:[~2023-08-10 11:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-10 11:39 Martin Uecker [this message]
2023-08-10 22:08 ` Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=573a12823f25ecf649457f32018f2f53a2a8b3a2.camel@tugraz.at \
    --to=uecker@tugraz.at \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).