[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-25 Thread Tyler Rockwood via cfe-commits

rockwotj wrote:

Changed 

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-24 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,91 @@
+// RUN: %check_clang_tidy -std=c++17 %s 
bugprone-unused-local-non-trivial-variable %t -- \
+// RUN:   -config="{CheckOptions: 
{bugprone-unused-local-non-trivial-variable.IncludeTypes: '::async::Future'}}"

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-24 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,91 @@
+// RUN: %check_clang_tidy -std=c++17 %s 
bugprone-unused-local-non-trivial-variable %t -- \

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-24 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,92 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex =
+"::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;"
+"::std::base_istringstream;::std::base_stringstream;::std::bitset;"
+"::std::path";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+AST_MATCHER(QualType, isTrivial) {
+  return Node.isTrivialType(Finder->getASTContext()) ||
+ Node.isTriviallyCopyableType(Finder->getASTContext());
+}
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypes(utils::options::parseStringList(
+  Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
+  ExcludeTypes(
+  utils::options::parseStringList(Options.get("ExcludeTypes", ""))) {}
+
+void UnusedLocalNonTrivialVariableCheck::storeOptions(
+ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "IncludeTypes",
+utils::options::serializeStringList(IncludeTypes));
+  Options.store(Opts, "ExcludeTypes",
+utils::options::serializeStringList(ExcludeTypes));
+}
+
+void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) 
{
+  if (IncludeTypes.empty())
+return;
+
+  Finder->addMatcher(
+  varDecl(isLocalVarDecl(), unless(isReferenced()),
+  unless(isExpansionInSystemHeader()),

rockwotj wrote:

SG removed.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-24 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,92 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex =
+"::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;"
+"::std::base_istringstream;::std::base_stringstream;::std::bitset;"
+"::std::path";

rockwotj wrote:

Yeah I don't use these APIs much, but good catch! I've fixed it.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-24 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From 6f0b7e5a80a8812e95357397384217dde4f81b01 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Thu, 21 Dec 2023 16:31:12 -0600
Subject: [PATCH 1/2] clang-tidy/bugprone: introduce
 unused-local-non-trivial-variable check

Signed-off-by: Tyler Rockwood 
---
 .../bugprone/BugproneTidyModule.cpp   |   3 +
 .../clang-tidy/bugprone/CMakeLists.txt|   1 +
 .../UnusedLocalNonTrivialVariableCheck.cpp|  87 +
 .../UnusedLocalNonTrivialVariableCheck.h  |  44 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../unused-local-non-trivial-variable.rst |  56 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../unused-local-non-trivial-variable.cpp | 114 ++
 8 files changed, 311 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 7a910037368c83..435cb1e3fbcff3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -83,6 +83,7 @@
 #include "UnhandledSelfAssignmentCheck.h"
 #include "UniquePtrArrayMismatchCheck.h"
 #include "UnsafeFunctionsCheck.h"
+#include "UnusedLocalNonTrivialVariableCheck.h"
 #include "UnusedRaiiCheck.h"
 #include "UnusedReturnValueCheck.h"
 #include "UseAfterMoveCheck.h"
@@ -235,6 +236,8 @@ class BugproneModule : public ClangTidyModule {
 "bugprone-unique-ptr-array-mismatch");
 CheckFactories.registerCheck(
 "bugprone-unsafe-functions");
+CheckFactories.registerCheck(
+"bugprone-unused-local-non-trivial-variable");
 CheckFactories.registerCheck("bugprone-unused-raii");
 CheckFactories.registerCheck(
 "bugprone-unused-return-value");
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index d443fd8d1452f1..70e7fbc7ec0c14 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -79,6 +79,7 @@ add_clang_library(clangTidyBugproneModule
   UnhandledSelfAssignmentCheck.cpp
   UniquePtrArrayMismatchCheck.cpp
   UnsafeFunctionsCheck.cpp
+  UnusedLocalNonTrivialVariableCheck.cpp
   UnusedRaiiCheck.cpp
   UnusedReturnValueCheck.cpp
   UseAfterMoveCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
new file mode 100644
index 00..18f975668b8cad
--- /dev/null
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -0,0 +1,87 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex =
+"::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;"
+"::std::base_istringstream;::std::base_stringstream;::std::bitset;"
+"::std::filesystem::path";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+AST_MATCHER(QualType, isTrivial) {
+  return Node.isTrivialType(Finder->getASTContext()) ||
+ Node.isTriviallyCopyableType(Finder->getASTContext());
+}
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypes(utils::options::parseStringList(
+  Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
+  

[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-24 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,89 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex = 
"std::.*mutex;std::future";

rockwotj wrote:

Done. I didn't add `std::basic_string_view` because string_view should be a 
trivial type.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-24 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From cf118a475b8dfef36c365c417c9cf63b79ff4055 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Thu, 21 Dec 2023 16:31:12 -0600
Subject: [PATCH 1/2] clang-tidy/bugprone: introduce
 unused-local-non-trivial-variable check

Signed-off-by: Tyler Rockwood 
---
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 .../UnusedLocalNonTrivialVariableCheck.cpp| 88 +++
 .../UnusedLocalNonTrivialVariableCheck.h  | 44 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../unused-local-non-trivial-variable.rst | 56 
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../unused-local-non-trivial-variable.cpp | 87 ++
 8 files changed, 285 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 7a910037368c83..435cb1e3fbcff3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -83,6 +83,7 @@
 #include "UnhandledSelfAssignmentCheck.h"
 #include "UniquePtrArrayMismatchCheck.h"
 #include "UnsafeFunctionsCheck.h"
+#include "UnusedLocalNonTrivialVariableCheck.h"
 #include "UnusedRaiiCheck.h"
 #include "UnusedReturnValueCheck.h"
 #include "UseAfterMoveCheck.h"
@@ -235,6 +236,8 @@ class BugproneModule : public ClangTidyModule {
 "bugprone-unique-ptr-array-mismatch");
 CheckFactories.registerCheck(
 "bugprone-unsafe-functions");
+CheckFactories.registerCheck(
+"bugprone-unused-local-non-trivial-variable");
 CheckFactories.registerCheck("bugprone-unused-raii");
 CheckFactories.registerCheck(
 "bugprone-unused-return-value");
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index d443fd8d1452f1..70e7fbc7ec0c14 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -79,6 +79,7 @@ add_clang_library(clangTidyBugproneModule
   UnhandledSelfAssignmentCheck.cpp
   UniquePtrArrayMismatchCheck.cpp
   UnsafeFunctionsCheck.cpp
+  UnusedLocalNonTrivialVariableCheck.cpp
   UnusedRaiiCheck.cpp
   UnusedReturnValueCheck.cpp
   UseAfterMoveCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
new file mode 100644
index 00..2d82c46a92ac12
--- /dev/null
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -0,0 +1,88 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex =
+"::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;"
+"::std::base_istringstream;::std::base_stringstream;::std::bitset;"
+"::std::path";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+AST_MATCHER(QualType, isTrivial) {
+  return Node.isTrivialType(Finder->getASTContext()) ||
+ Node.isTriviallyCopyableType(Finder->getASTContext());
+}
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypes(utils::options::parseStringList(
+  Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
+  

[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,45 @@
+.. title:: clang-tidy - bugprone-unused-local-non-trivial-variable
+
+bugprone-unused-local-non-trivial-variable
+==
+
+Warns when a local non trivial variable is unused within a function.
+The following types of variables are excluded from this check:
+
+* trivial and trivially copyable
+* references and pointers
+* exception variables in catch clauses
+* static or thread local
+* structured bindings
+
+This check can be configured to warn on all non-trivial variables by setting 
`ExcludeTypes` to `.*`.

rockwotj wrote:

Fixed thanks.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From 9a9fe7fd0a7b54c90042c64aa9db23b4ca703ec0 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Thu, 21 Dec 2023 16:31:12 -0600
Subject: [PATCH 1/2] clang-tidy/bugprone: introduce
 unused-local-non-trivial-variable check

Signed-off-by: Tyler Rockwood 
---
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 .../UnusedLocalNonTrivialVariableCheck.cpp| 86 ++
 .../UnusedLocalNonTrivialVariableCheck.h  | 44 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../unused-local-non-trivial-variable.rst | 47 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../unused-local-non-trivial-variable.cpp | 87 +++
 8 files changed, 274 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 7a910037368c83..435cb1e3fbcff3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -83,6 +83,7 @@
 #include "UnhandledSelfAssignmentCheck.h"
 #include "UniquePtrArrayMismatchCheck.h"
 #include "UnsafeFunctionsCheck.h"
+#include "UnusedLocalNonTrivialVariableCheck.h"
 #include "UnusedRaiiCheck.h"
 #include "UnusedReturnValueCheck.h"
 #include "UseAfterMoveCheck.h"
@@ -235,6 +236,8 @@ class BugproneModule : public ClangTidyModule {
 "bugprone-unique-ptr-array-mismatch");
 CheckFactories.registerCheck(
 "bugprone-unsafe-functions");
+CheckFactories.registerCheck(
+"bugprone-unused-local-non-trivial-variable");
 CheckFactories.registerCheck("bugprone-unused-raii");
 CheckFactories.registerCheck(
 "bugprone-unused-return-value");
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index d443fd8d1452f1..70e7fbc7ec0c14 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -79,6 +79,7 @@ add_clang_library(clangTidyBugproneModule
   UnhandledSelfAssignmentCheck.cpp
   UniquePtrArrayMismatchCheck.cpp
   UnsafeFunctionsCheck.cpp
+  UnusedLocalNonTrivialVariableCheck.cpp
   UnusedRaiiCheck.cpp
   UnusedReturnValueCheck.cpp
   UseAfterMoveCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
new file mode 100644
index 00..5ad3bb03c58a60
--- /dev/null
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -0,0 +1,86 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex =
+"::std::.*mutex;::std::future;::std::string;::std::regex";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+AST_MATCHER(QualType, isTrivial) {
+  return Node.isTrivialType(Finder->getASTContext()) ||
+ Node.isTriviallyCopyableType(Finder->getASTContext());
+}
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypes(utils::options::parseStringList(
+  Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
+  ExcludeTypes(
+  utils::options::parseStringList(Options.get("ExcludeTypes", ""))) {}
+
+void 

[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From fff68e1854d16a166088d7199af09a7aeb19b4c4 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Thu, 21 Dec 2023 16:31:12 -0600
Subject: [PATCH 1/2] clang-tidy/bugprone: introduce
 unused-local-non-trivial-variable check

Signed-off-by: Tyler Rockwood 
---
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 .../UnusedLocalNonTrivialVariableCheck.cpp| 86 ++
 .../UnusedLocalNonTrivialVariableCheck.h  | 44 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../unused-local-non-trivial-variable.rst | 45 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../unused-local-non-trivial-variable.cpp | 87 +++
 8 files changed, 272 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 7a910037368c83..435cb1e3fbcff3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -83,6 +83,7 @@
 #include "UnhandledSelfAssignmentCheck.h"
 #include "UniquePtrArrayMismatchCheck.h"
 #include "UnsafeFunctionsCheck.h"
+#include "UnusedLocalNonTrivialVariableCheck.h"
 #include "UnusedRaiiCheck.h"
 #include "UnusedReturnValueCheck.h"
 #include "UseAfterMoveCheck.h"
@@ -235,6 +236,8 @@ class BugproneModule : public ClangTidyModule {
 "bugprone-unique-ptr-array-mismatch");
 CheckFactories.registerCheck(
 "bugprone-unsafe-functions");
+CheckFactories.registerCheck(
+"bugprone-unused-local-non-trivial-variable");
 CheckFactories.registerCheck("bugprone-unused-raii");
 CheckFactories.registerCheck(
 "bugprone-unused-return-value");
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index d443fd8d1452f1..70e7fbc7ec0c14 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -79,6 +79,7 @@ add_clang_library(clangTidyBugproneModule
   UnhandledSelfAssignmentCheck.cpp
   UniquePtrArrayMismatchCheck.cpp
   UnsafeFunctionsCheck.cpp
+  UnusedLocalNonTrivialVariableCheck.cpp
   UnusedRaiiCheck.cpp
   UnusedReturnValueCheck.cpp
   UseAfterMoveCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
new file mode 100644
index 00..5ad3bb03c58a60
--- /dev/null
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -0,0 +1,86 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex =
+"::std::.*mutex;::std::future;::std::string;::std::regex";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+AST_MATCHER(QualType, isTrivial) {
+  return Node.isTrivialType(Finder->getASTContext()) ||
+ Node.isTriviallyCopyableType(Finder->getASTContext());
+}
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypes(utils::options::parseStringList(
+  Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
+  ExcludeTypes(
+  utils::options::parseStringList(Options.get("ExcludeTypes", ""))) {}
+
+void 

[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,79 @@
+// RUN: %check_clang_tidy %s bugprone-unused-local-non-trivial-variable %t -- \
+// RUN:   -config="{CheckOptions: 
{bugprone-unused-local-non-trivial-variable.IncludeTypeRegex: 
'::async::Future'}}"
+
+
+namespace async {
+template 
+class Ptr {
+  public:
+  explicit Ptr(T Arg) : Underlying(new T(Arg)) {}
+  T& operator->() {
+return Underlying;
+  }
+  ~Ptr() {
+delete Underlying;
+  }
+  private:
+T* Underlying;
+};
+
+template
+class Future {
+public:
+T get() {
+return Pending;
+}
+~Future();
+private:
+T Pending;
+};
+
+
+} // namespace async
+
+// Warning is still emitted if there are type aliases.
+namespace a {
+template
+using Future = async::Future;
+} // namespace a
+
+void releaseUnits();
+struct Units {
+  ~Units() {
+releaseUnits();
+  }
+};
+a::Future acquireUnits();
+
+template
+T qux(T Generic) {
+async::Future PendingA = acquireUnits();
+auto PendingB = acquireUnits();
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: unused local variable 
'PendingB' of type 'a::Future' (aka 'Future') 
[bugprone-unused-local-non-trivial-variable]
+async::Future MustBeUsed;
+// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: unused local variable 
'MustBeUsed' of type 'async::Future' 
[bugprone-unused-local-non-trivial-variable]
+PendingA.get();
+return Generic;
+}
+
+async::Future Global;
+
+int bar(int Num) {
+a::Future PendingA = acquireUnits();
+a::Future PendingB = acquireUnits(); // not used at all, unused 
variable not fired because of destructor side effect
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: unused local variable 
'PendingB' of type 'a::Future' (aka 'Future') 
[bugprone-unused-local-non-trivial-variable]
+auto Num2 = PendingA.get();
+auto Num3 = qux(Num);
+async::Ptr> Shared = 
async::Ptr>(acquireUnits());
+static auto UnusedStatic = async::Future();
+thread_local async::Future UnusedThreadLocal;
+auto Captured = acquireUnits();
+Num3 += [Captured]() {
+  return 1;
+}();
+a::Future Referenced = acquireUnits();
+a::Future* Pointer = 
+a::Future& Reference = Referenced;
+const a::Future& ConstReference = Referenced;
+return Num * Num3;
+}

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,79 @@
+// RUN: %check_clang_tidy %s bugprone-unused-local-non-trivial-variable %t -- \
+// RUN:   -config="{CheckOptions: 
{bugprone-unused-local-non-trivial-variable.IncludeTypeRegex: 
'::async::Future'}}"
+
+
+namespace async {
+template 
+class Ptr {
+  public:
+  explicit Ptr(T Arg) : Underlying(new T(Arg)) {}
+  T& operator->() {
+return Underlying;
+  }
+  ~Ptr() {
+delete Underlying;
+  }
+  private:
+T* Underlying;
+};
+
+template
+class Future {
+public:
+T get() {
+return Pending;
+}
+~Future();
+private:
+T Pending;
+};
+
+
+} // namespace async
+
+// Warning is still emitted if there are type aliases.
+namespace a {
+template
+using Future = async::Future;
+} // namespace a
+
+void releaseUnits();
+struct Units {
+  ~Units() {
+releaseUnits();
+  }
+};
+a::Future acquireUnits();
+
+template
+T qux(T Generic) {
+async::Future PendingA = acquireUnits();
+auto PendingB = acquireUnits();
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: unused local variable 
'PendingB' of type 'a::Future' (aka 'Future') 
[bugprone-unused-local-non-trivial-variable]
+async::Future MustBeUsed;
+// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: unused local variable 
'MustBeUsed' of type 'async::Future' 
[bugprone-unused-local-non-trivial-variable]
+PendingA.get();
+return Generic;
+}
+
+async::Future Global;
+
+int bar(int Num) {
+a::Future PendingA = acquireUnits();
+a::Future PendingB = acquireUnits(); // not used at all, unused 
variable not fired because of destructor side effect
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: unused local variable 
'PendingB' of type 'a::Future' (aka 'Future') 
[bugprone-unused-local-non-trivial-variable]
+auto Num2 = PendingA.get();
+auto Num3 = qux(Num);
+async::Ptr> Shared = 
async::Ptr>(acquireUnits());
+static auto UnusedStatic = async::Future();
+thread_local async::Future UnusedThreadLocal;
+auto Captured = acquireUnits();
+Num3 += [Captured]() {
+  return 1;
+}();
+a::Future Referenced = acquireUnits();
+a::Future* Pointer = 
+a::Future& Reference = Referenced;
+const a::Future& ConstReference = Referenced;
+return Num * Num3;
+}

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,89 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex = 
"std::.*mutex;std::future";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypeRegex(utils::options::parseStringList(
+  Options.get("IncludeTypeRegex", DefaultIncludeTypeRegex))),
+  ExcludeTypeRegex(utils::options::parseStringList(
+  Options.get("ExcludeTypeRegex", ""))) {}
+
+void UnusedLocalNonTrivialVariableCheck::storeOptions(
+ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "IncludeTypeRegex",
+utils::options::serializeStringList(IncludeTypeRegex));
+  Options.store(Opts, "ExcludeTypeRegex",
+utils::options::serializeStringList(ExcludeTypeRegex));
+}
+
+void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) 
{
+  if (IncludeTypeRegex.empty())
+return;
+
+  Finder->addMatcher(
+  varDecl(
+  isLocalVarDecl(), unless(isReferenced()),
+  unless(isExpansionInSystemHeader()), unless(isImplicit()),
+  unless(isExceptionVariable()), hasLocalStorage(), isDefinition(),
+  unless(hasType(isReferenceType())),
+  hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+  recordDecl(matchesAnyListedName(IncludeTypeRegex)),
+  unless(hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+  recordDecl(matchesAnyListedName(ExcludeTypeRegex

rockwotj wrote:

Done, thanks!

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -149,6 +149,7 @@ Clang-Tidy Checks
:doc:`bugprone-unhandled-self-assignment 
`,
:doc:`bugprone-unique-ptr-array-mismatch 
`, "Yes"
:doc:`bugprone-unsafe-functions `,
+   :doc:`bugprone-unused-local-non-trivial-variable 
`, "Yes"

rockwotj wrote:

Done, thanks.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,89 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex = 
"std::.*mutex;std::future";

rockwotj wrote:

Added std::string and std::regex, let me know if you think of others.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,29 @@
+.. title:: clang-tidy - bugprone-unused-local-non-trivial-variable
+
+bugprone-unused-local-non-trivial-variable
+==
+
+Warns when a local non trivial variable is unused within a function.
+
+In the following example, `future2` would generate a warning that it is unused.
+
+.. code-block:: c++
+

rockwotj wrote:

Took a shot at this. PTAL.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,89 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex = 
"std::.*mutex;std::future";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypeRegex(utils::options::parseStringList(
+  Options.get("IncludeTypeRegex", DefaultIncludeTypeRegex))),
+  ExcludeTypeRegex(utils::options::parseStringList(
+  Options.get("ExcludeTypeRegex", ""))) {}
+
+void UnusedLocalNonTrivialVariableCheck::storeOptions(
+ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "IncludeTypeRegex",
+utils::options::serializeStringList(IncludeTypeRegex));
+  Options.store(Opts, "ExcludeTypeRegex",
+utils::options::serializeStringList(ExcludeTypeRegex));
+}
+
+void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) 
{
+  if (IncludeTypeRegex.empty())
+return;
+
+  Finder->addMatcher(
+  varDecl(
+  isLocalVarDecl(), unless(isReferenced()),
+  unless(isExpansionInSystemHeader()), unless(isImplicit()),

rockwotj wrote:

Thanks, done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,89 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex = 
"std::.*mutex;std::future";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypeRegex(utils::options::parseStringList(
+  Options.get("IncludeTypeRegex", DefaultIncludeTypeRegex))),

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-22 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From fff68e1854d16a166088d7199af09a7aeb19b4c4 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Thu, 21 Dec 2023 16:31:12 -0600
Subject: [PATCH] clang-tidy/bugprone: introduce
 unused-local-non-trivial-variable check

Signed-off-by: Tyler Rockwood 
---
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 .../UnusedLocalNonTrivialVariableCheck.cpp| 86 ++
 .../UnusedLocalNonTrivialVariableCheck.h  | 44 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../unused-local-non-trivial-variable.rst | 45 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../unused-local-non-trivial-variable.cpp | 87 +++
 8 files changed, 272 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 7a910037368c83..435cb1e3fbcff3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -83,6 +83,7 @@
 #include "UnhandledSelfAssignmentCheck.h"
 #include "UniquePtrArrayMismatchCheck.h"
 #include "UnsafeFunctionsCheck.h"
+#include "UnusedLocalNonTrivialVariableCheck.h"
 #include "UnusedRaiiCheck.h"
 #include "UnusedReturnValueCheck.h"
 #include "UseAfterMoveCheck.h"
@@ -235,6 +236,8 @@ class BugproneModule : public ClangTidyModule {
 "bugprone-unique-ptr-array-mismatch");
 CheckFactories.registerCheck(
 "bugprone-unsafe-functions");
+CheckFactories.registerCheck(
+"bugprone-unused-local-non-trivial-variable");
 CheckFactories.registerCheck("bugprone-unused-raii");
 CheckFactories.registerCheck(
 "bugprone-unused-return-value");
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index d443fd8d1452f1..70e7fbc7ec0c14 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -79,6 +79,7 @@ add_clang_library(clangTidyBugproneModule
   UnhandledSelfAssignmentCheck.cpp
   UniquePtrArrayMismatchCheck.cpp
   UnsafeFunctionsCheck.cpp
+  UnusedLocalNonTrivialVariableCheck.cpp
   UnusedRaiiCheck.cpp
   UnusedReturnValueCheck.cpp
   UseAfterMoveCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
new file mode 100644
index 00..5ad3bb03c58a60
--- /dev/null
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -0,0 +1,86 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex =
+"::std::.*mutex;::std::future;::std::string;::std::regex";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+AST_MATCHER(QualType, isTrivial) {
+  return Node.isTrivialType(Finder->getASTContext()) ||
+ Node.isTriviallyCopyableType(Finder->getASTContext());
+}
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypes(utils::options::parseStringList(
+  Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
+  ExcludeTypes(
+  utils::options::parseStringList(Options.get("ExcludeTypes", ""))) {}
+
+void 

[clang-tools-extra] [clang-tidy] introduce a unused local non trival variable check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj edited 
https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,29 @@
+.. title:: clang-tidy - bugprone-unused-local-non-trivial-variable
+
+bugprone-unused-local-non-trivial-variable
+==
+
+Warns when a local non trivial variable is unused within a function.
+
+In the following example, `future2` would generate a warning that it is unused.
+
+.. code-block:: c++
+
+   std::future future1;
+   std::future future2;
+   // ...
+   MyObject foo = future1.get();
+   // future2 is not used.
+
+Options
+---
+
+.. option:: IncludeTypeRegex
+
+   Semicolon-separated list of regular expressions matching types of variables 
to check.
+   By default it 'std::.*mutex;std::future'.
+
+.. option:: ExcludeTypeRegex
+
+   A semicolon-separated list of regular expressions matching types that are 
excluded from the
+  'IncludeTypeRegex' matches. By default it is an empty list.

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -168,6 +168,19 @@ New checks
   extracted from an optional-like type and then used to create a new instance
   of the same optional-like type.
 
+- New :doc:`bugprone-unused-local-non-trivial-variable
+  ` check.
+
+  Warns when a local non trivial variable is unused within a function. By

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,29 @@
+.. title:: clang-tidy - bugprone-unused-local-non-trivial-variable
+
+bugprone-unused-local-non-trivial-variable
+==
+
+Warns when a local non trivial variable is unused within a function.
+
+In the following example, `future2` would generate a warning that it is unused.
+
+.. code-block:: c++
+
+   std::future future1;
+   std::future future2;
+   // ...
+   MyObject foo = future1.get();
+   // future2 is not used.
+
+Options
+---
+
+.. option:: IncludeTypeRegex
+
+   Semicolon-separated list of regular expressions matching types of variables 
to check.
+   By default it 'std::.*mutex;std::future'.

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From 90d92d028833d25c218a9f184b5e1407500c9d01 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Thu, 21 Dec 2023 16:31:12 -0600
Subject: [PATCH] clang-tidy/bugprone: introduce
 unused-local-non-trivial-variable check

Signed-off-by: Tyler Rockwood 
---
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 .../UnusedLocalNonTrivialVariableCheck.cpp| 89 +++
 .../UnusedLocalNonTrivialVariableCheck.h  | 44 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../unused-local-non-trivial-variable.rst | 29 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../unused-local-non-trivial-variable.cpp | 79 
 8 files changed, 251 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 7a910037368c83..435cb1e3fbcff3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -83,6 +83,7 @@
 #include "UnhandledSelfAssignmentCheck.h"
 #include "UniquePtrArrayMismatchCheck.h"
 #include "UnsafeFunctionsCheck.h"
+#include "UnusedLocalNonTrivialVariableCheck.h"
 #include "UnusedRaiiCheck.h"
 #include "UnusedReturnValueCheck.h"
 #include "UseAfterMoveCheck.h"
@@ -235,6 +236,8 @@ class BugproneModule : public ClangTidyModule {
 "bugprone-unique-ptr-array-mismatch");
 CheckFactories.registerCheck(
 "bugprone-unsafe-functions");
+CheckFactories.registerCheck(
+"bugprone-unused-local-non-trivial-variable");
 CheckFactories.registerCheck("bugprone-unused-raii");
 CheckFactories.registerCheck(
 "bugprone-unused-return-value");
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index d443fd8d1452f1..70e7fbc7ec0c14 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -79,6 +79,7 @@ add_clang_library(clangTidyBugproneModule
   UnhandledSelfAssignmentCheck.cpp
   UniquePtrArrayMismatchCheck.cpp
   UnsafeFunctionsCheck.cpp
+  UnusedLocalNonTrivialVariableCheck.cpp
   UnusedRaiiCheck.cpp
   UnusedReturnValueCheck.cpp
   UseAfterMoveCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
new file mode 100644
index 00..2c61078e1d1cc1
--- /dev/null
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -0,0 +1,89 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex = 
"std::.*mutex;std::future";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypeRegex(utils::options::parseStringList(
+  Options.get("IncludeTypeRegex", DefaultIncludeTypeRegex))),
+  ExcludeTypeRegex(utils::options::parseStringList(
+  Options.get("ExcludeTypeRegex", ""))) {}
+
+void UnusedLocalNonTrivialVariableCheck::storeOptions(
+ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "IncludeTypeRegex",
+utils::options::serializeStringList(IncludeTypeRegex));
+  

[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,28 @@
+.. title:: clang-tidy - misc-must-use
+
+misc-must-use
+=
+
+Allow strictly enforcing that variables are used for specific classes,
+even with they would not be normally warned using `-Wunused-variable` due 

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,48 @@
+// RUN: %check_clang_tidy %s misc-must-use %t -- \
+// RUN:   -config="{CheckOptions: [{key: 'misc-must-use.Types', value: 
'::async::Future'}]}"

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,40 @@
+//===--- MustUseCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MUSTUSECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MUSTUSECHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::misc {
+
+/// Warns when not using a variable of a specific type within a function. This
+/// enforces a stronger check than clang's unused-variable warnings, as in C++
+/// this warning is not fired if the class has a custom destructor, or in
+/// templates. This check allows re-enabling unused variable warnings in all
+/// situations for specific classes.
+///
+/// The check supports this option:
+///   - 'Types': a semicolon-separated list of types to ensure must be used.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc/must-use.html
+class MustUseCheck : public ClangTidyCheck {
+public:
+  MustUseCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  void storeOptions(ClangTidyOptions::OptionMap ) override;
+

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,48 @@
+// RUN: %check_clang_tidy %s misc-must-use %t -- \
+// RUN:   -config="{CheckOptions: [{key: 'misc-must-use.Types', value: 
'::async::Future'}]}"
+
+namespace async {
+template
+class Future {
+public:
+T get() {
+return Pending;
+}
+private:
+T Pending;
+};
+
+
+} // namespace async
+
+// Warning is still emitted if there are type aliases.
+namespace a {
+template
+using Future = async::Future;
+} // namespace a
+
+void releaseUnits();
+struct Units {
+  ~Units() {
+releaseUnits();
+  }
+};
+a::Future acquireUnits();
+
+template
+T qux(T Generic) {
+async::Future PendingA = acquireUnits();
+auto PendingB = acquireUnits();
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'PendingB' must be 
used [misc-must-use]
+PendingA.get();
+return Generic;
+}
+
+int bar(int Num) {
+a::Future PendingA = acquireUnits();
+a::Future PendingB = acquireUnits(); // not used at all, unused 
variable not fired because of destructor side effect
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: variable 'PendingB' must be 
used [misc-must-use]
+auto Num2 = PendingA.get();
+auto Num3 = qux(Num);
+return Num * Num3;
+}

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,28 @@
+.. title:: clang-tidy - misc-must-use
+
+misc-must-use
+=
+
+Allow strictly enforcing that variables are used for specific classes,
+even with they would not be normally warned using `-Wunused-variable` due 
+templates or custom destructors.
+
+In the following example, `future2` normally would not trigger any unused 
variable checks,
+but using `{key: "misc-must-use.Types", value: "std::future"}` would cause 
`future2` to have

rockwotj wrote:

Thanks TIL, fixed.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,49 @@
+//===--- MustUseCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "MustUseCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+namespace {
+AST_MATCHER(VarDecl, isLocalVar) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+} // namespace
+
+MustUseCheck::MustUseCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  Types(utils::options::parseStringList(Options.get("Types", ""))) {}
+
+void MustUseCheck::registerMatchers(MatchFinder *Finder) {
+  if (Types.empty()) {
+return;
+  }
+  Finder->addMatcher(
+  varDecl(isLocalVar(), unless(isReferenced()),
+  hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+  recordDecl(matchers::matchesAnyListedName(Types)))
+  .bind("var"),
+  this);
+}
+
+void MustUseCheck::check(const MatchFinder::MatchResult ) {
+  const auto *MatchedDecl = Result.Nodes.getNodeAs("var");
+  diag(MatchedDecl->getLocation(), "variable %0 must be used") << MatchedDecl;

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,40 @@
+//===--- MustUseCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MUSTUSECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MUSTUSECHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::misc {
+
+/// Warns when not using a variable of a specific type within a function. This
+/// enforces a stronger check than clang's unused-variable warnings, as in C++
+/// this warning is not fired if the class has a custom destructor, or in
+/// templates. This check allows re-enabling unused variable warnings in all
+/// situations for specific classes.
+///
+/// The check supports this option:
+///   - 'Types': a semicolon-separated list of types to ensure must be used.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc/must-use.html
+class MustUseCheck : public ClangTidyCheck {
+public:
+  MustUseCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  void storeOptions(ClangTidyOptions::OptionMap ) override;
+

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,49 @@
+//===--- MustUseCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "MustUseCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+namespace {
+AST_MATCHER(VarDecl, isLocalVar) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+} // namespace
+
+MustUseCheck::MustUseCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  Types(utils::options::parseStringList(Options.get("Types", ""))) {}
+
+void MustUseCheck::registerMatchers(MatchFinder *Finder) {
+  if (Types.empty()) {

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -54,6 +55,7 @@ class MiscModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "misc-misleading-identifier");
 CheckFactories.registerCheck("misc-misplaced-const");
+CheckFactories.registerCheck("misc-must-use");

rockwotj wrote:

SG added most of these checks, let me know if you think I should add any.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From 5afeaab9f148b10d951e37fd27cb32687f310a9c Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Thu, 21 Dec 2023 16:31:12 -0600
Subject: [PATCH] clang-tidy/bugprone: introduce
 unused-local-non-trivial-variable check

Signed-off-by: Tyler Rockwood 
---
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 .../UnusedLocalNonTrivialVariableCheck.cpp| 89 +++
 .../UnusedLocalNonTrivialVariableCheck.h  | 44 +
 clang-tools-extra/docs/ReleaseNotes.rst   | 13 +++
 .../unused-local-non-trivial-variable.rst | 29 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../unused-local-non-trivial-variable.cpp | 79 
 8 files changed, 259 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 7a910037368c83..435cb1e3fbcff3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -83,6 +83,7 @@
 #include "UnhandledSelfAssignmentCheck.h"
 #include "UniquePtrArrayMismatchCheck.h"
 #include "UnsafeFunctionsCheck.h"
+#include "UnusedLocalNonTrivialVariableCheck.h"
 #include "UnusedRaiiCheck.h"
 #include "UnusedReturnValueCheck.h"
 #include "UseAfterMoveCheck.h"
@@ -235,6 +236,8 @@ class BugproneModule : public ClangTidyModule {
 "bugprone-unique-ptr-array-mismatch");
 CheckFactories.registerCheck(
 "bugprone-unsafe-functions");
+CheckFactories.registerCheck(
+"bugprone-unused-local-non-trivial-variable");
 CheckFactories.registerCheck("bugprone-unused-raii");
 CheckFactories.registerCheck(
 "bugprone-unused-return-value");
diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
index d443fd8d1452f1..70e7fbc7ec0c14 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
@@ -79,6 +79,7 @@ add_clang_library(clangTidyBugproneModule
   UnhandledSelfAssignmentCheck.cpp
   UniquePtrArrayMismatchCheck.cpp
   UnsafeFunctionsCheck.cpp
+  UnusedLocalNonTrivialVariableCheck.cpp
   UnusedRaiiCheck.cpp
   UnusedReturnValueCheck.cpp
   UseAfterMoveCheck.cpp
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
new file mode 100644
index 00..2c61078e1d1cc1
--- /dev/null
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -0,0 +1,89 @@
+//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UnusedLocalNonTrivialVariableCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tidy::matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+static constexpr StringRef DefaultIncludeTypeRegex = 
"std::.*mutex;std::future";
+
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
+} // namespace
+
+UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  IncludeTypeRegex(utils::options::parseStringList(
+  Options.get("IncludeTypeRegex", DefaultIncludeTypeRegex))),
+  ExcludeTypeRegex(utils::options::parseStringList(
+  Options.get("ExcludeTypeRegex", ""))) {}
+
+void UnusedLocalNonTrivialVariableCheck::storeOptions(
+ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "IncludeTypeRegex",
+utils::options::serializeStringList(IncludeTypeRegex));
+  

[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -54,6 +55,7 @@ class MiscModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "misc-misleading-identifier");
 CheckFactories.registerCheck("misc-misplaced-const");
+CheckFactories.registerCheck("misc-must-use");

rockwotj wrote:

Is there a difference between isReferenced versus the following:

```
decl().bind("var"), 
hasAncestor(functionDecl(unless(hasDescendant(declRefExpr(to(decl(equalsBoundNode("var"
```

Sorry still new to AST matchers

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -54,6 +55,7 @@ class MiscModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "misc-misleading-identifier");
 CheckFactories.registerCheck("misc-misplaced-const");
+CheckFactories.registerCheck("misc-must-use");

rockwotj wrote:

Merging that check and mine STGM (as well as the naming of your check). Do you 
have source for your check you'd like to point to? Otherwise I'm happy to add 
in a lot of these.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,28 @@
+.. title:: clang-tidy - misc-must-use
+
+misc-must-use
+=
+
+Allows for strictly enforce variables are used for specific classes, even with
+they would not be normally warned using -Wunused-variable due to templates or
+custom destructors.
+
+In the following example, future2 normally would not trigger any unused 
variable checks,

rockwotj wrote:

Thanks done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -187,6 +187,13 @@ New checks
   points in a coroutine. Such hostile types include scoped-lockable types and
   types belonging to a configurable denylist.
 
+- New :doc:`misc-must-use
+  ` check.
+
+  Add the ability to strictly enforce variables are used for specific classes,
+  even with they would not be normally warned using -Wunused-variable due 

rockwotj wrote:

Thanks, done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits


@@ -0,0 +1,28 @@
+.. title:: clang-tidy - misc-must-use
+
+misc-must-use
+=
+
+Allows for strictly enforce variables are used for specific classes, even with

rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-21 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From 07158b0396be8eb1d551bc98b538c5d23c42d9e9 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Wed, 20 Dec 2023 15:05:48 -0600
Subject: [PATCH] clang-tidy/misc: introduce a must use check

Introduce a new (off by default) clang tidy check to ensure that
variables of a specific type are always used even if -Wunused-variables
wouldn't generate a warning.

This check has already caught a couple of different bugs on the codebase
I work on, where not handling a future means that lifetimes may not be
kept alive properly as an async chunk of code may run after a class has
been destroyed, etc.

I would like to upstream it because I believe there could be other
applications of this check that would be useful in different contexts.
The check itself is quite simple.

Signed-off-by: Tyler Rockwood 
---
 .../clang-tidy/misc/CMakeLists.txt|  1 +
 .../clang-tidy/misc/MiscTidyModule.cpp|  2 +
 .../clang-tidy/misc/MustUseCheck.cpp  | 49 +++
 .../clang-tidy/misc/MustUseCheck.h| 40 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  7 +++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../docs/clang-tidy/checks/misc/must-use.rst  | 28 +++
 .../clang-tidy/checkers/misc/must-use.cpp | 48 ++
 8 files changed, 176 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/misc/MustUseCheck.h
 create mode 100644 clang-tools-extra/docs/clang-tidy/checks/misc/must-use.rst
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/must-use.cpp

diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
index d9ec268650c053..374b4fc049c452 100644
--- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyMiscModule
   MisleadingBidirectional.cpp
   MisleadingIdentifier.cpp
   MisplacedConstCheck.cpp
+  MustUseCheck.cpp
   NewDeleteOverloadsCheck.cpp
   NoRecursionCheck.cpp
   NonCopyableObjects.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp 
b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
index d8a88324ee63e0..651c859c153755 100644
--- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
@@ -18,6 +18,7 @@
 #include "MisleadingBidirectional.h"
 #include "MisleadingIdentifier.h"
 #include "MisplacedConstCheck.h"
+#include "MustUseCheck.h"
 #include "NewDeleteOverloadsCheck.h"
 #include "NoRecursionCheck.h"
 #include "NonCopyableObjects.h"
@@ -54,6 +55,7 @@ class MiscModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "misc-misleading-identifier");
 CheckFactories.registerCheck("misc-misplaced-const");
+CheckFactories.registerCheck("misc-must-use");
 CheckFactories.registerCheck(
 "misc-new-delete-overloads");
 CheckFactories.registerCheck("misc-no-recursion");
diff --git a/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
new file mode 100644
index 00..31870994e4a9f8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
@@ -0,0 +1,49 @@
+//===--- MustUseCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "MustUseCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+namespace {
+AST_MATCHER(VarDecl, isLocalVar) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+} // namespace
+
+MustUseCheck::MustUseCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  Types(utils::options::parseStringList(Options.get("Types", ""))) {}
+
+void MustUseCheck::registerMatchers(MatchFinder *Finder) {
+  if (Types.empty()) {
+return;
+  }
+  Finder->addMatcher(
+  varDecl(isLocalVar(), unless(isReferenced()),
+  hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+  recordDecl(matchers::matchesAnyListedName(Types)))
+  .bind("var"),
+  this);
+}
+
+void MustUseCheck::check(const MatchFinder::MatchResult ) {
+  const auto *MatchedDecl = Result.Nodes.getNodeAs("var");
+  diag(MatchedDecl->getLocation(), "variable %0 must be used") << MatchedDecl;
+}
+
+void 

[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-20 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj updated 
https://github.com/llvm/llvm-project/pull/76101

>From 34feb9d9fefc4007a657366824ad38221228e86f Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Wed, 20 Dec 2023 15:05:48 -0600
Subject: [PATCH] clang-tidy/misc: introduce a must use check

Introduce a new (off by default) clang tidy check to ensure that
variables of a specific type are always used even if -Wunused-variables
wouldn't generate a warning.

This check has already caught a couple of different bugs on the codebase
I work on, where not handling a future means that lifetimes may not be
kept alive properly as an async chunk of code may run after a class has
been destroyed, etc.

I would like to upstream it because I believe there could be other
applications of this check that would be useful in different contexts.
The check itself is quite simple.

Signed-off-by: Tyler Rockwood 
---
 .../clang-tidy/misc/CMakeLists.txt|  1 +
 .../clang-tidy/misc/MiscTidyModule.cpp|  2 +
 .../clang-tidy/misc/MustUseCheck.cpp  | 49 +++
 .../clang-tidy/misc/MustUseCheck.h| 40 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  7 +++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../docs/clang-tidy/checks/misc/must-use.rst  | 28 +++
 .../clang-tidy/checkers/misc/must-use.cpp | 48 ++
 8 files changed, 176 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/misc/MustUseCheck.h
 create mode 100644 clang-tools-extra/docs/clang-tidy/checks/misc/must-use.rst
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/must-use.cpp

diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
index d9ec268650c053..374b4fc049c452 100644
--- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyMiscModule
   MisleadingBidirectional.cpp
   MisleadingIdentifier.cpp
   MisplacedConstCheck.cpp
+  MustUseCheck.cpp
   NewDeleteOverloadsCheck.cpp
   NoRecursionCheck.cpp
   NonCopyableObjects.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp 
b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
index d8a88324ee63e0..651c859c153755 100644
--- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
@@ -18,6 +18,7 @@
 #include "MisleadingBidirectional.h"
 #include "MisleadingIdentifier.h"
 #include "MisplacedConstCheck.h"
+#include "MustUseCheck.h"
 #include "NewDeleteOverloadsCheck.h"
 #include "NoRecursionCheck.h"
 #include "NonCopyableObjects.h"
@@ -54,6 +55,7 @@ class MiscModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "misc-misleading-identifier");
 CheckFactories.registerCheck("misc-misplaced-const");
+CheckFactories.registerCheck("misc-must-use");
 CheckFactories.registerCheck(
 "misc-new-delete-overloads");
 CheckFactories.registerCheck("misc-no-recursion");
diff --git a/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
new file mode 100644
index 00..31870994e4a9f8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
@@ -0,0 +1,49 @@
+//===--- MustUseCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "MustUseCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+namespace {
+AST_MATCHER(VarDecl, isLocalVar) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+} // namespace
+
+MustUseCheck::MustUseCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  Types(utils::options::parseStringList(Options.get("Types", ""))) {}
+
+void MustUseCheck::registerMatchers(MatchFinder *Finder) {
+  if (Types.empty()) {
+return;
+  }
+  Finder->addMatcher(
+  varDecl(isLocalVar(), unless(isReferenced()),
+  hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+  recordDecl(matchers::matchesAnyListedName(Types)))
+  .bind("var"),
+  this);
+}
+
+void MustUseCheck::check(const MatchFinder::MatchResult ) {
+  const auto *MatchedDecl = Result.Nodes.getNodeAs("var");
+  diag(MatchedDecl->getLocation(), "variable %0 must be used") << MatchedDecl;
+}
+
+void 

[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

2023-12-20 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj edited 
https://github.com/llvm/llvm-project/pull/76101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] clang-tidy/misc: introduce a must use check (PR #76101)

2023-12-20 Thread Tyler Rockwood via cfe-commits

https://github.com/rockwotj created 
https://github.com/llvm/llvm-project/pull/76101

Introduce a new (off by default) clang tidy check to ensure that variables of a 
specific type are always used even if -Wunused-variables wouldn't generate a 
warning.

This check has already caught a couple of different bugs on the codebase I work 
on, where not handling a future means that lifetimes may not be kept alive 
properly as an async chunk of code may run after a class has been destroyed, 
etc.

I would like to upstream it because I believe there could be other applications 
of this check that would be useful in different contexts.

>From f7f94494372c4e9b42d027b6fc100e90f2bfe1e0 Mon Sep 17 00:00:00 2001
From: Tyler Rockwood 
Date: Wed, 20 Dec 2023 15:05:48 -0600
Subject: [PATCH] clang-tidy/misc: introduce a must use check

Introduce a new (off by default) clang tidy check to ensure that
variables of a specific type are always used even if -Wunused-variables
wouldn't generate a warning.

This check has already caught a couple of different bugs on the codebase
I work on, where not handling a future means that lifetimes may not be
kept alive properly as an async chunk of code may run after a class has
been destroyed, etc.

I would like to upstream it because I believe there could be other
applications of this check that would be useful in different contexts.
The check itself is quite simple.

Signed-off-by: Tyler Rockwood 
---
 .../clang-tidy/misc/CMakeLists.txt|  1 +
 .../clang-tidy/misc/MiscTidyModule.cpp|  3 ++
 .../clang-tidy/misc/MustUseCheck.cpp  | 49 +++
 .../clang-tidy/misc/MustUseCheck.h| 40 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  7 +++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../docs/clang-tidy/checks/misc/must-use.rst  | 28 +++
 .../clang-tidy/checkers/misc/must-use.cpp | 48 ++
 8 files changed, 177 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/misc/MustUseCheck.h
 create mode 100644 clang-tools-extra/docs/clang-tidy/checks/misc/must-use.rst
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/must-use.cpp

diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
index d9ec268650c053..374b4fc049c452 100644
--- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangTidyMiscModule
   MisleadingBidirectional.cpp
   MisleadingIdentifier.cpp
   MisplacedConstCheck.cpp
+  MustUseCheck.cpp
   NewDeleteOverloadsCheck.cpp
   NoRecursionCheck.cpp
   NonCopyableObjects.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp 
b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
index d8a88324ee63e0..9f7cf912fbc0d5 100644
--- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
@@ -18,6 +18,7 @@
 #include "MisleadingBidirectional.h"
 #include "MisleadingIdentifier.h"
 #include "MisplacedConstCheck.h"
+#include "MustUseCheck.h"
 #include "NewDeleteOverloadsCheck.h"
 #include "NoRecursionCheck.h"
 #include "NonCopyableObjects.h"
@@ -54,6 +55,8 @@ class MiscModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "misc-misleading-identifier");
 CheckFactories.registerCheck("misc-misplaced-const");
+CheckFactories.registerCheck(
+"misc-must-use");
 CheckFactories.registerCheck(
 "misc-new-delete-overloads");
 CheckFactories.registerCheck("misc-no-recursion");
diff --git a/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
new file mode 100644
index 00..31870994e4a9f8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misc/MustUseCheck.cpp
@@ -0,0 +1,49 @@
+//===--- MustUseCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "MustUseCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+namespace {
+AST_MATCHER(VarDecl, isLocalVar) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+} // namespace
+
+MustUseCheck::MustUseCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  Types(utils::options::parseStringList(Options.get("Types", ""))) {}
+
+void