Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

#include "UseTrailingReturnTypeCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTTypeTraits.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Tooling/FixIt.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -457,6 +460,7 @@ static void keepSpecifiers(std::string &ReturnType, std::string &Auto,
UseTrailingReturnTypeCheck::UseTrailingReturnTypeCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
WarnOnNonTrailingVoid(Options.get("WarnOnNonTrailingVoid", false)),
TransformFunctions(Options.get("TransformFunctions", true)),
TransformLambdas(Options.get("TransformLambdas", TransformLambda::All)) {

Expand All @@ -469,18 +473,23 @@ UseTrailingReturnTypeCheck::UseTrailingReturnTypeCheck(

void UseTrailingReturnTypeCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "WarnOnNonTrailingVoid", WarnOnNonTrailingVoid);
Options.store(Opts, "TransformFunctions", TransformFunctions);
Options.store(Opts, "TransformLambdas", TransformLambdas);
}

void UseTrailingReturnTypeCheck::registerMatchers(MatchFinder *Finder) {
const auto HasNoWrittenReturnType =
anyOf(cxxConversionDecl(), cxxConstructorDecl(), cxxDestructorDecl());

auto F =
functionDecl(
unless(anyOf(
hasTrailingReturn(), returns(voidType()), cxxConversionDecl(),
traverse(
TK_IgnoreUnlessSpelledInSource,
functionDecl(unless(anyOf(
hasTrailingReturn(), HasNoWrittenReturnType,
WarnOnNonTrailingVoid ? unless(anything()) : returns(voidType()),
cxxMethodDecl(
anyOf(isImplicit(),
hasParent(cxxRecordDecl(hasParent(lambdaExpr()))))))))
hasParent(cxxRecordDecl(hasParent(lambdaExpr()))))))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This last hasParent won't work. cxxRecordDecl::get parent returns a DeclContext, which can not be a lambda expression.

.bind("Func");

if (TransformFunctions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class UseTrailingReturnTypeCheck : public ClangTidyCheck {

private:
Preprocessor *PP = nullptr;
const bool WarnOnNonTrailingVoid;
const bool TransformFunctions;
const TransformLambda TransformLambdas;

Expand Down
10 changes: 10 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ Changes in existing checks
- Improved :doc:`misc-header-include-cycle
<clang-tidy/checks/misc/header-include-cycle>` check performance.

- Improved :doc:`modernize-use-trailing-return-type
<clang-tidy/checks/modernize/use-trailing-return-type>` check by adding the
option `WarnOnNonTrailingVoid` that applies the check to ``void``-returning
functions that by default are excluded from this check.

- Improved :doc:`modernize-use-designated-initializers
<clang-tidy/checks/modernize/use-designated-initializers>` check to
suggest using designated initializers for aliased aggregate types.
Expand Down Expand Up @@ -190,6 +195,11 @@ Changes in existing checks
<clang-tidy/checks/readability/qualified-auto>` check by adding the option
`IgnoreAliasing`, that allows not looking at underlying types of type aliases.

- Improved :doc:`performance-unnecessary-value-param
<clang-tidy/checks/performance/unnecessary-value-param>` check performance by
tolerating fix-it breaking compilation when functions is used as pointers
to avoid matching usage of functions within the current compilation unit.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ transforms to:
virtual auto f3() const && -> float = delete;
auto lambda = []() -> void {};

Options
-------

.. option:: WarnOnNonTrailingVoid

If the option is set to `true`, the check will apply even to function
signatures with return type ``void``. Default is `false`.
Comment on lines +30 to +36
Copy link
Contributor

@vbvictor vbvictor Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already an "options" paragraph, don't create a new one and add to the existing.


Limitations
-----------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-use-trailing-return-type %t -- -- -fdeclspec -fexceptions -DCOMMAND_LINE_INT=int
// RUN: %check_clang_tidy \
// RUN: -std=c++14-or-later %s modernize-use-trailing-return-type %t -- \
// RUN: -- -fdeclspec -fexceptions -DCOMMAND_LINE_INT=int
// RUN: %check_clang_tidy -check-suffixes=,WARN-ON-NONTRAILING-VOID \
// RUN: -std=c++14-or-later %s modernize-use-trailing-return-type %t -- \
// RUN: -config="{CheckOptions: { \
// RUN: modernize-use-trailing-return-type.WarnOnNonTrailingVoid: true, \
// RUN: }}" \
// RUN: -- -fdeclspec -fexceptions -DCOMMAND_LINE_INT=int

namespace std {
template <typename T>
Expand Down Expand Up @@ -566,6 +574,24 @@ ostream& operator<<(ostream& ostream, int i);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
// CHECK-FIXES: {{^}}ostream& operator<<(ostream& ostream, int i);{{$}}

//
// WarnOnNonTrailingVoid option
//

void leadingVoid();
// CHECK-MESSAGES-WARN-ON-NONTRAILING-VOID: :[[@LINE-1]]:6: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
// CHECK-FIXES-WARN-ON-NONTRAILING-VOID: {{^}}auto leadingVoid() -> void;{{$}}
void leadingVoid(int arg);
// CHECK-MESSAGES-WARN-ON-NONTRAILING-VOID: :[[@LINE-1]]:6: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
// CHECK-FIXES-WARN-ON-NONTRAILING-VOID: {{^}}auto leadingVoid(int arg) -> void;{{$}}
void leadingVoid(int arg) { return; }
// CHECK-MESSAGES-WARN-ON-NONTRAILING-VOID: :[[@LINE-1]]:6: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
// CHECK-FIXES-WARN-ON-NONTRAILING-VOID: {{^}}auto leadingVoid(int arg) -> void { return; }{{$}}

auto trailingVoid() -> void;
auto trailingVoid(int arg) -> void;
auto trailingVoid(int arg) -> void { return; }

//
// Samples which do not trigger the check
//
Expand All @@ -579,10 +605,6 @@ template <typename T> auto f(T t) -> int;

auto ff();

void c();
void c(int arg);
void c(int arg) { return; }

struct D2 : B {
D2();
virtual ~D2();
Expand Down