]> granicus.if.org Git - clang/commitdiff
Improve the diagnostic emitted when an unused ObjC property getter
authorChris Lattner <sabre@nondot.org>
Sun, 16 Aug 2009 16:57:27 +0000 (16:57 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 16 Aug 2009 16:57:27 +0000 (16:57 +0000)
is found.  Instead of complaining about a generic "unused expr",
emit:
t.m:7:3: warning: property access result unused - getters should not have side effects

While objc property getters *could* have side effects, according to
the language best practices, they *shouldn't*.  Hopefully the
diagnostic now gets this across.

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaStmt.cpp
test/SemaObjC/unused.m

index 2ebcfd36f5b9cab67937e82de83f840ced0fdf98..b5f03526774816bb9107e3412a375987e9c9d549 100644 (file)
@@ -1687,6 +1687,9 @@ def ext_typecheck_expression_not_constant_but_accepted : Extension<
   "expression is not a constant, but is accepted as one by GNU extensions">;
 def warn_unused_expr : Warning<"expression result unused">,
   InGroup<UnusedValue>;
+def warn_unused_property_expr : Warning<
+  "property access result unused - getters should not have side effects">,
+  InGroup<UnusedValue>;
 
 def err_incomplete_type_used_in_type_trait_expr : Error<
   "incomplete type %0 used in type trait expression">;
index f66ee1e30f2e2c0697902584698e293ac0ebb162..579433849e6c7f0df91c3b24e5ee02d0b27f0606 100644 (file)
@@ -15,7 +15,7 @@
 #include "clang/AST/APValue.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclObjC.h"
-#include "clang/AST/Expr.h"
+#include "clang/AST/ExprObjC.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/StmtCXX.h"
 #include "clang/Basic/TargetInfo.h"
@@ -65,7 +65,15 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
   if (!E->isUnusedResultAWarning(Loc, R1, R2))
     return;
   
-  Diag(Loc, diag::warn_unused_expr) << R1 << R2;
+  // Okay, we have an unused result.  Depending on what the base expression is,
+  // we might want to make a more specific diagnostic.  Check for one of these
+  // cases now.
+  unsigned DiagID = diag::warn_unused_expr;
+  E = E->IgnoreParens();
+  if (isa<ObjCKVCRefExpr>(E))
+    DiagID = diag::warn_unused_property_expr;
+  
+  Diag(Loc, DiagID) << R1 << R2;
 }
 
 Action::OwningStmtResult
index ce77cf1b5f564fae57787fbf62264fda0bc0a28d..bbe3109a28173d617fe6f0906c53c9f52ee4aebd 100644 (file)
@@ -25,7 +25,7 @@ int test1(void) {
 @end
 
 void test2() {
-  @"pointless example call for test purposes".length; // expected-warning {{expression result unused}}
+  @"pointless example call for test purposes".length; // expected-warning {{property access result unused - getters should not have side effects}}
 }