]> granicus.if.org Git - clang/commitdiff
Devote a special diagnostic to the typo
authorJohn McCall <rjmccall@apple.com>
Tue, 6 Apr 2010 22:24:14 +0000 (22:24 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 6 Apr 2010 22:24:14 +0000 (22:24 +0000)
  (void*) someFunction(5, 10, 15, 20);
where the cast is presumably meant to be to 'void'.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@100574 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaStmt.cpp
test/Sema/unused-expr.c

index b66d6cc6e5a357c7b4dfe15552615a83125a315d..ebd3a4e252ed9187587d9387efc5e132e04c205b 100644 (file)
@@ -2359,6 +2359,9 @@ def ext_typecheck_expression_not_constant_but_accepted : Extension<
   InGroup<GNU>;
 def warn_unused_expr : Warning<"expression result unused">,
   InGroup<UnusedValue>;
+def warn_unused_voidptr : Warning<
+  "expression result unused; should this cast be to 'void'?">,
+  InGroup<UnusedValue>;
 def warn_unused_property_expr : Warning<
   "property access result unused - getters should not have side effects">,
   InGroup<UnusedValue>;
index 791de8c2b9fa60b5f0152c9756d846e7498d40a2..0c55cf5c5cf874c61946be1b7b5e66260af2ac28 100644 (file)
@@ -20,6 +20,7 @@
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/StmtCXX.h"
+#include "clang/AST/TypeLoc.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/STLExtras.h"
@@ -126,6 +127,22 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
       return;
     }
   }
+
+  // Diagnose "(void*) blah" as a typo for "(void) blah".
+  else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
+    TypeSourceInfo *TI = CE->getTypeInfoAsWritten();
+    QualType T = TI->getType();
+
+    // We really do want to use the non-canonical type here.
+    if (T == Context.VoidPtrTy) {
+      PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc());
+
+      Diag(Loc, diag::warn_unused_voidptr)
+        << FixItHint::CreateRemoval(TL.getStarLoc());
+      return;
+    }
+  }
+
   Diag(Loc, DiagID) << R1 << R2;
 }
 
index 4c6c47b34517045518286c6eebf3db64a500e1dc..4ae0d4b46504a8787e9f09bbf5582a2521c464a9 100644 (file)
@@ -104,3 +104,9 @@ void t8() {
 }
 
 void t9() __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}}
+
+// rdar://7410924
+void *some_function(void);
+void t10() {
+  (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}}
+}