[PATCH] D151383: Check for specific return types on all functions

2023-05-24 Thread NagaChaitanya Vellanki via Phabricator via cfe-commits
chaitanyav added a comment.

@royjacobson Please list other types that must be included here. The tests are 
coming...


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151383/new/

https://reviews.llvm.org/D151383

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D151383: Check for specific return types on all functions

2023-05-24 Thread NagaChaitanya Vellanki via Phabricator via cfe-commits
chaitanyav created this revision.
chaitanyav added a reviewer: royjacobson.
Herald added subscribers: PiotrZSL, carlosgalvezp.
Herald added a reviewer: njames93.
Herald added a project: All.
chaitanyav requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Extend the check to all functions with return types like

  std::error_code, std::expected, boost::system::error_code, abseil::Status...
  
  Resolves issue https://github.com/llvm/llvm-project/issues/62884


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151383

Files:
  clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp


Index: clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -156,6 +156,15 @@
   auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
   auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
 
+  Finder->addMatcher(
+  callExpr(callee(functionDecl(anyOf(
+   hasReturnTypeLoc(loc(asString("std::error_code"))),
+   hasReturnTypeLoc(loc(asString("std::expected"))),
+   
hasReturnTypeLoc(loc(asString("boost::system::error_code"))),
+   hasReturnTypeLoc(loc(asString("abseil::Status")))
+  .bind("return-types"),
+  this);
+
   Finder->addMatcher(
   stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
  UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
@@ -164,6 +173,16 @@
 }
 
 void UnusedReturnValueCheck::check(const MatchFinder::MatchResult ) {
+
+  if (const auto *Matched = Result.Nodes.getNodeAs("return-types")) {
+diag(Matched->getBeginLoc(),
+ "the value returned by this function should be used")
+<< Matched->getSourceRange();
+diag(Matched->getBeginLoc(),
+ "cast the expression to void to silence this warning",
+ DiagnosticIDs::Note);
+  };
+
   if (const auto *Matched = Result.Nodes.getNodeAs("match")) {
 diag(Matched->getBeginLoc(),
  "the value returned by this function should be used")


Index: clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -156,6 +156,15 @@
   auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
   auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
 
+  Finder->addMatcher(
+  callExpr(callee(functionDecl(anyOf(
+   hasReturnTypeLoc(loc(asString("std::error_code"))),
+   hasReturnTypeLoc(loc(asString("std::expected"))),
+   hasReturnTypeLoc(loc(asString("boost::system::error_code"))),
+   hasReturnTypeLoc(loc(asString("abseil::Status")))
+  .bind("return-types"),
+  this);
+
   Finder->addMatcher(
   stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
  UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
@@ -164,6 +173,16 @@
 }
 
 void UnusedReturnValueCheck::check(const MatchFinder::MatchResult ) {
+
+  if (const auto *Matched = Result.Nodes.getNodeAs("return-types")) {
+diag(Matched->getBeginLoc(),
+ "the value returned by this function should be used")
+<< Matched->getSourceRange();
+diag(Matched->getBeginLoc(),
+ "cast the expression to void to silence this warning",
+ DiagnosticIDs::Note);
+  };
+
   if (const auto *Matched = Result.Nodes.getNodeAs("match")) {
 diag(Matched->getBeginLoc(),
  "the value returned by this function should be used")
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits