[clang-tools-extra] clang-tidy: readability-redundant-smartptr-get does not remove (#97964) (PR #100177)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 24 13:15:29 PDT 2024
https://github.com/akshaykumars614 updated https://github.com/llvm/llvm-project/pull/100177
>From 642e558be0a1362f2b5cc4180dab7729f0ba8528 Mon Sep 17 00:00:00 2001
From: akshaykumars614 <akshaykumars614 at gmail.com>
Date: Tue, 23 Jul 2024 14:32:24 -0400
Subject: [PATCH 1/4] clang-tidy: readability-redundant-smartptr-get does not
remove (#97964)
added a check to remove '->' if exists
added testcase and modified Release Notes
---
.../readability/RedundantSmartptrGetCheck.cpp | 4 ++
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++
.../readability/redundant-smartptr-get.cpp | 72 +++++++++++++++++++
3 files changed, 80 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
index 8837ac16e8828..be52af77ae0a5 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
@@ -164,6 +164,10 @@ void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
StringRef SmartptrText = Lexer::getSourceText(
CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
*Result.SourceManager, getLangOpts());
+ // Check if the last two characters are "->" and remove them
+ if (SmartptrText.ends_with("->")) {
+ SmartptrText = SmartptrText.drop_back(2);
+ }
// Replace foo->get() with *foo, and foo.get() with foo.
std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer")
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a23483e6df6d2..374e89b8226a6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -498,6 +498,10 @@ Changes in existing checks
false-positives when type of the member does not match the type of the
initializer.
+- Improved :doc:`readability-redundant-smartptr-get
+ <clang-tidy/checks/readability/redundant-smartptr-get>` check to
+ remove '->' when reduntant get() is removed.
+
- Improved :doc:`readability-static-accessed-through-instance
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
support calls to overloaded operators as base expression and provide fixes to
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
index 01f12b6bfe6ea..e9ecc06d53959 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
@@ -20,6 +20,47 @@ struct shared_ptr {
explicit operator bool() const noexcept;
};
+template <typename T>
+struct vector {
+ vector();
+ bool operator==(const vector<T>& other) const;
+ bool operator!=(const vector<T>& other) const;
+ unsigned long size() const;
+ bool empty() const;
+
+ // Basic iterator implementation for testing
+ struct iterator {
+ T* ptr;
+ iterator(T* p) : ptr(p) {}
+ T& operator*() { return *ptr; }
+ T* operator->() { return ptr; }
+ iterator& operator++() {
+ ++ptr;
+ return *this;
+ }
+ bool operator!=(const iterator& other) const { return ptr != other.ptr; }
+ };
+
+ iterator begin();
+ iterator end();
+
+ T* data;
+ unsigned long sz;
+};
+
+template <typename T>
+vector<T>::vector() : data(nullptr), sz(0) {}
+
+template <typename T>
+typename vector<T>::iterator vector<T>::begin() {
+ return iterator(data);
+}
+
+template <typename T>
+typename vector<T>::iterator vector<T>::end() {
+ return iterator(data + sz);
+}
+
} // namespace std
struct Bar {
@@ -235,3 +276,34 @@ void Negative() {
if (MACRO(x) == nullptr)
;
}
+
+void test_redundant_get() {
+ std::vector<std::shared_ptr<int>> v;
+ auto f = [](int) {};
+ for (auto i = v.begin(); i != v.end(); ++i) {
+ f(*i->get());
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
+ // CHECK-FIXES: f(**i);
+ }
+}
+
+struct Inner {
+ int a;
+ int *getValue() { return &a; }
+};
+
+struct Example {
+ Inner inner;
+ Inner* get() { return &inner; }
+ int *getValue() { return inner.getValue(); }
+};
+
+void test_redundant_get_with_member() {
+ std::vector<std::shared_ptr<Example>> v;
+ auto f = [](int) {};
+ for (auto i = v.begin(); i != v.end(); ++i) {
+ f(*(*i).get()->get()->getValue());
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
+ // CHECK-FIXES: f(**i->get()->getValue());
+ }
+}
>From 0421519378d819ce9c872efefb71096caad744eb Mon Sep 17 00:00:00 2001
From: akshaykumars614 <akshaykumars614 at gmail.com>
Date: Tue, 23 Jul 2024 15:45:27 -0400
Subject: [PATCH 2/4] clang-tidy: readability-redundant-smartptr-get does not
remove (#97964)
updated Release Notes
---
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 374e89b8226a6..48eae358075ff 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -500,7 +500,7 @@ Changes in existing checks
- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
- remove '->' when reduntant get() is removed.
+ remove '->', when reduntant get() is removed.
- Improved :doc:`readability-static-accessed-through-instance
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
>From f2afb94f5299dfb02d8b3acb4689aba888d9b04b Mon Sep 17 00:00:00 2001
From: akshaykumars614 <akshaykumars614 at gmail.com>
Date: Tue, 23 Jul 2024 17:30:02 -0400
Subject: [PATCH 3/4] clang-tidy: readability-redundant-smartptr-get does not
remove (#97964)
simplified testcase
---
.../readability/redundant-smartptr-get.cpp | 26 +------------------
1 file changed, 1 insertion(+), 25 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
index e9ecc06d53959..ec4ca4cb79484 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp
@@ -28,18 +28,7 @@ struct vector {
unsigned long size() const;
bool empty() const;
- // Basic iterator implementation for testing
- struct iterator {
- T* ptr;
- iterator(T* p) : ptr(p) {}
- T& operator*() { return *ptr; }
- T* operator->() { return ptr; }
- iterator& operator++() {
- ++ptr;
- return *this;
- }
- bool operator!=(const iterator& other) const { return ptr != other.ptr; }
- };
+ using iterator = T*;
iterator begin();
iterator end();
@@ -48,19 +37,6 @@ struct vector {
unsigned long sz;
};
-template <typename T>
-vector<T>::vector() : data(nullptr), sz(0) {}
-
-template <typename T>
-typename vector<T>::iterator vector<T>::begin() {
- return iterator(data);
-}
-
-template <typename T>
-typename vector<T>::iterator vector<T>::end() {
- return iterator(data + sz);
-}
-
} // namespace std
struct Bar {
>From acdb284f8fa7db04b404f6141c2623148974077d Mon Sep 17 00:00:00 2001
From: akshaykumars614 <88362922+akshaykumars614 at users.noreply.github.com>
Date: Wed, 24 Jul 2024 16:15:20 -0400
Subject: [PATCH 4/4] Update ReleaseNotes.rst
---
clang-tools-extra/docs/ReleaseNotes.rst | 290 ------------------------
1 file changed, 290 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index b3d72a61c054a..642ad39cc0c1c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -104,296 +104,6 @@ New check aliases
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^
-- Improved :doc:`bugprone-assert-side-effect
- <clang-tidy/checks/bugprone/assert-side-effect>` check by detecting side
- effect from calling a method with non-const reference parameters.
-
-- Improved :doc:`bugprone-assignment-in-if-condition
- <clang-tidy/checks/bugprone/assignment-in-if-condition>` check by ignoring
- assignments in the C++20 ``requires`` clause.
-
-- Improved :doc:`bugprone-casting-through-void
- <clang-tidy/checks/bugprone/casting-through-void>` check by ignoring casts
- where source is already a ``void``` pointer, making middle ``void`` pointer
- casts bug-free.
-
-- Improved :doc:`bugprone-forwarding-reference-overload
- <clang-tidy/checks/bugprone/forwarding-reference-overload>`
- check to ignore deleted constructors which won't hide other overloads.
-
-- Improved :doc:`bugprone-implicit-widening-of-multiplication-result
- <clang-tidy/checks/bugprone/implicit-widening-of-multiplication-result>` check
- by adding an option to ignore constant expressions of signed integer types
- that fit in the source expression type.
-
-- Improved :doc:`bugprone-inc-dec-in-conditions
- <clang-tidy/checks/bugprone/inc-dec-in-conditions>` check to ignore code
- within unevaluated contexts, such as ``decltype``.
-
-- Improved :doc:`bugprone-lambda-function-name<clang-tidy/checks/bugprone/lambda-function-name>`
- check by ignoring ``__func__`` macro in lambda captures, initializers of
- default parameters and nested function declarations.
-
-- Improved :doc:`bugprone-multi-level-implicit-pointer-conversion
- <clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion>` check
- by ignoring implicit pointer conversions that are part of a cast expression.
-
-- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
- <clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check by
- eliminating false positives resulting from direct usage of bitwise operators
- within parentheses.
-
-- Improved :doc:`bugprone-optional-value-conversion
- <clang-tidy/checks/bugprone/optional-value-conversion>` check by eliminating
- false positives resulting from use of optionals in unevaluated context.
-
-- Improved :doc:`bugprone-sizeof-expression
- <clang-tidy/checks/bugprone/sizeof-expression>` check by clarifying the
- diagnostics, eliminating some false positives and adding a new
- (off-by-default) option `WarnOnSizeOfPointer` that reports all
- ``sizeof(pointer)`` expressions (except for a few that are idiomatic).
-
-- Improved :doc:`bugprone-suspicious-include
- <clang-tidy/checks/bugprone/suspicious-include>` check by replacing the local
- options `HeaderFileExtensions` and `ImplementationFileExtensions` by the
- global options of the same name.
-
-- Improved :doc:`bugprone-too-small-loop-variable
- <clang-tidy/checks/bugprone/too-small-loop-variable>` check by incorporating
- better support for ``const`` loop boundaries.
-
-- Improved :doc:`bugprone-unused-local-non-trivial-variable
- <clang-tidy/checks/bugprone/unused-local-non-trivial-variable>` check by
- ignoring local variable with ``[maybe_unused]`` attribute.
-
-- Improved :doc:`bugprone-unused-return-value
- <clang-tidy/checks/bugprone/unused-return-value>` check by updating the
- parameter `CheckedFunctions` to support regexp, avoiding false positive for
- function with the same prefix as the default argument, e.g. ``std::unique_ptr``
- and ``std::unique``, avoiding false positive for assignment operator overloading.
-
-- Improved :doc:`bugprone-use-after-move
- <clang-tidy/checks/bugprone/use-after-move>` check to also handle
- calls to ``std::forward``. Fixed sequencing of designated initializers. Fixed
- sequencing of callees: In C++17 and later, the callee of a function is guaranteed
- to be sequenced before the arguments, so don't warn if the use happens in the
- callee and the move happens in one of the arguments.
-
-- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables
- <clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables>` check
- with a new option `AllowInternalLinkage` to disable the warning for variables
- with internal linkage.
-
-- Improved :doc:`cppcoreguidelines-macro-usage
- <clang-tidy/checks/cppcoreguidelines/macro-usage>` check by ignoring macro with
- hash preprocessing token.
-
-- Improved :doc:`cppcoreguidelines-missing-std-forward
- <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by no longer
- giving false positives for deleted functions, by fixing false negatives when only
- a few parameters are forwarded and by ignoring parameters without a name (unused
- arguments).
-
-- Improved :doc:`cppcoreguidelines-owning-memory
- <clang-tidy/checks/cppcoreguidelines/owning-memory>` check to properly handle
- return type in lambdas and in nested functions.
-
-- Improved :doc:`cppcoreguidelines-prefer-member-initializer
- <clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check
- by removing enforcement of rule `C.48
- <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers>`_,
- which was deprecated since :program:`clang-tidy` 17. This rule is now covered
- by :doc:`cppcoreguidelines-use-default-member-init
- <clang-tidy/checks/cppcoreguidelines/use-default-member-init>`. Fixed
- incorrect hints when using list-initialization.
-
-- Improved :doc:`cppcoreguidelines-special-member-functions
- <clang-tidy/checks/cppcoreguidelines/special-member-functions>` check with a
- new option `AllowImplicitlyDeletedCopyOrMove`, which removes the requirement
- for explicit copy or move special member functions when they are already
- implicitly deleted.
-
-- Improved :doc:`google-build-namespaces
- <clang-tidy/checks/google/build-namespaces>` check by replacing the local
- option `HeaderFileExtensions` by the global option of the same name.
-
-- Improved :doc:`google-explicit-constructor
- <clang-tidy/checks/google/explicit-constructor>` check to better handle
- C++20 `explicit(bool)`.
-
-- Improved :doc:`google-global-names-in-headers
- <clang-tidy/checks/google/global-names-in-headers>` check by replacing the local
- option `HeaderFileExtensions` by the global option of the same name.
-
-- Improved :doc:`google-runtime-int <clang-tidy/checks/google/runtime-int>`
- check performance through optimizations.
-
-- Improved :doc:`hicpp-signed-bitwise <clang-tidy/checks/hicpp/signed-bitwise>`
- check by ignoring false positives involving positive integer literals behind
- implicit casts when `IgnorePositiveIntegerLiterals` is enabled.
-
-- Improved :doc:`hicpp-ignored-remove-result <clang-tidy/checks/hicpp/ignored-remove-result>`
- check by ignoring other functions with same prefixes as the target specific
- functions.
-
-- Improved :doc:`linuxkernel-must-check-errs
- <clang-tidy/checks/linuxkernel/must-check-errs>` check documentation to
- consistently use the check's proper name.
-
-- Improved :doc:`llvm-header-guard
- <clang-tidy/checks/llvm/header-guard>` check by replacing the local
- option `HeaderFileExtensions` by the global option of the same name.
-
-- Improved :doc:`misc-const-correctness
- <clang-tidy/checks/misc/const-correctness>` check by avoiding infinite recursion
- for recursive functions with forwarding reference parameters and reference
- variables which refer to themselves.
-
-- Improved :doc:`misc-definitions-in-headers
- <clang-tidy/checks/misc/definitions-in-headers>` check by replacing the local
- option `HeaderFileExtensions` by the global option of the same name.
- Additionally, the option `UseHeaderFileExtensions` is removed, so that the
- check uses the `HeaderFileExtensions` option unconditionally.
-
-- Improved :doc:`misc-header-include-cycle
- <clang-tidy/checks/misc/header-include-cycle>` check by avoiding crash for self
- include cycles.
-
-- Improved :doc:`misc-unused-using-decls
- <clang-tidy/checks/misc/unused-using-decls>` check by replacing the local
- option `HeaderFileExtensions` by the global option of the same name.
-
-- Improved :doc:`misc-use-anonymous-namespace
- <clang-tidy/checks/misc/use-anonymous-namespace>` check by replacing the local
- option `HeaderFileExtensions` by the global option of the same name.
-
-- Improved :doc:`modernize-avoid-c-arrays
- <clang-tidy/checks/modernize/avoid-c-arrays>` check by introducing the new
- `AllowStringArrays` option, enabling the exclusion of array types with deduced
- length initialized from string literals.
-
-- Improved :doc:`modernize-loop-convert
- <clang-tidy/checks/modernize/loop-convert>` check by ensuring that fix-its
- don't remove parentheses used in ``sizeof`` calls when they have array index
- accesses as arguments.
-
-- Improved :doc:`modernize-use-constraints
- <clang-tidy/checks/modernize/use-constraints>` check by fixing a crash that
- occurred in some scenarios and excluding system headers from analysis.
-
-- Improved :doc:`modernize-use-nullptr
- <clang-tidy/checks/modernize/use-nullptr>` check to include support for C23,
- which also has introduced the ``nullptr`` keyword.
-
-- Improved :doc:`modernize-use-override
- <clang-tidy/checks/modernize/use-override>` check to also remove any trailing
- whitespace when deleting the ``virtual`` keyword.
-
-- Improved :doc:`modernize-use-starts-ends-with
- <clang-tidy/checks/modernize/use-starts-ends-with>` check to also handle
- calls to ``compare`` method.
-
-- Improved :doc:`modernize-use-std-print
- <clang-tidy/checks/modernize/use-std-print>` check to not crash if the
- format string parameter of the function to be replaced is not of the
- expected type.
-
-- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
- check by adding support for detection of typedefs declared on function level.
-
-- Improved :doc:`performance-inefficient-vector-operation
- <clang-tidy/checks/performance/inefficient-vector-operation>` fixing false
- negatives caused by different variable definition type and variable initial
- value type in loop initialization expression.
-
-- Improved :doc:`performance-move-const-arg
- <clang-tidy/checks/performance/move-const-arg>` check by ignoring
- ``std::move()`` calls when their target is used as an rvalue.
-
-- Improved :doc:`performance-unnecessary-copy-initialization
- <clang-tidy/checks/performance/unnecessary-copy-initialization>` check by
- detecting more cases of constant access. In particular, pointers can be
- analyzed, so the check now handles the common patterns
- `const auto e = (*vector_ptr)[i]` and `const auto e = vector_ptr->at(i);`.
- Calls to mutable function where there exists a `const` overload are also
- handled. Fix crash in the case of a non-member operator call.
-
-- Improved :doc:`performance-unnecessary-value-param
- <clang-tidy/checks/performance/unnecessary-value-param>` check
- detecting more cases for template functions including lambdas with ``auto``.
- E.g., ``std::sort(a.begin(), a.end(), [](auto x, auto y) { return a > b; });``
- will be detected for expensive to copy types. Fixed false positives for
- dependent call expressions.
-
-- Improved :doc:`readability-avoid-return-with-void-value
- <clang-tidy/checks/readability/avoid-return-with-void-value>` check by adding
- fix-its.
-
-- Improved :doc:`readability-const-return-type
- <clang-tidy/checks/readability/const-return-type>` check to eliminate false
- positives when returning types with const not at the top level.
-
-- Improved :doc:`readability-container-size-empty
- <clang-tidy/checks/readability/container-size-empty>` check to prevent false
- positives when utilizing ``size`` or ``length`` methods that accept parameter.
- Fixed crash when facing template user defined literals.
-
-- Improved :doc:`readability-duplicate-include
- <clang-tidy/checks/readability/duplicate-include>` check by excluding include
- directives that form the filename using macro.
-
-- Improved :doc:`readability-else-after-return
- <clang-tidy/checks/readability/else-after-return>` check to ignore
- `if consteval` statements, for which the `else` branch must not be removed.
-
-- Improved :doc:`readability-identifier-naming
- <clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
- mode by resolving symbolic links to header files. Fixed handling of Hungarian
- Prefix when configured to `LowerCase`. Added support for renaming designated
- initializers. Added support for renaming macro arguments. Fixed renaming
- conflicts arising from out-of-line member function template definitions.
-
-- Improved :doc:`readability-implicit-bool-conversion
- <clang-tidy/checks/readability/implicit-bool-conversion>` check to provide
- valid fix suggestions for ``static_cast`` without a preceding space and
- fixed problem with duplicate parentheses in double implicit casts. Corrected
- the fix suggestions for C23 and later by using C-style casts instead of
- ``static_cast``. Fixed false positives in C++20 spaceship operator by ignoring
- casts in implicit and defaulted functions.
-
-- Improved :doc:`readability-redundant-inline-specifier
- <clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
- emit warnings for static data member with an in-class initializer.
-
-- Improved :doc:`readability-redundant-member-init
- <clang-tidy/checks/readability/redundant-member-init>` check to avoid
- false-positives when type of the member does not match the type of the
- initializer.
-
-- Improved :doc:`readability-redundant-smartptr-get
- <clang-tidy/checks/readability/redundant-smartptr-get>` check to
- remove '->', when reduntant get() is removed.
-
-- Improved :doc:`readability-static-accessed-through-instance
- <clang-tidy/checks/readability/static-accessed-through-instance>` check to
- support calls to overloaded operators as base expression and provide fixes to
- expressions with side-effects.
-
-- Improved :doc:`readability-simplify-boolean-expr
- <clang-tidy/checks/readability/simplify-boolean-expr>` check to avoid to emit
- warning for macro when IgnoreMacro option is enabled and improve messages
- when auto-fix does not work.
-
-- Improved :doc:`readability-static-definition-in-anonymous-namespace
- <clang-tidy/checks/readability/static-definition-in-anonymous-namespace>`
- check by resolving fix-it overlaps in template code by disregarding implicit
- instances.
-
-- Improved :doc:`readability-string-compare
- <clang-tidy/checks/readability/string-compare>` check to also detect
- usages of ``std::string_view::compare``. Added a `StringLikeClasses` option
- to detect usages of ``compare`` method in custom string-like classes.
-
Removed checks
^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list