Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] False positive bugprone-exception-escape for throwing lambda in noexcept function #132605

Open
Eisenwave opened this issue Mar 23, 2025 · 4 comments
Labels
clang-tidy false-positive Warning fires when it should not

Comments

@Eisenwave
Copy link

https://godbolt.org/z/1sf8Gne35

void awoo() noexcept {
    [] { throw 0; };
}

This outputs

<source>:1:6: warning: an exception may be thrown in function 'awoo' which should not throw exceptions [bugprone-exception-escape]
    1 | void awoo() noexcept {
      |      ^
@llvmbot
Copy link
Member

llvmbot commented Mar 23, 2025

@llvm/issue-subscribers-clang-tidy

Author: Jan Schultke (Eisenwave)

https://godbolt.org/z/1sf8Gne35 ```cpp void awoo() noexcept { [] { throw 0; }; } ``` This outputs ``` <source>:1:6: warning: an exception may be thrown in function 'awoo' which should not throw exceptions [bugprone-exception-escape] 1 | void awoo() noexcept { | ^ ```

@EugeneZelenko EugeneZelenko added the false-positive Warning fires when it should not label Mar 23, 2025
@denzor200
Copy link

I tried to reproduce the issue in a simplified clang-query request. Does anybody can explain why the simple matcher functionDecl(isNoThrow()) triggers on lambda like []() noexcept(false) { throw 0; }; ? While it doesn't trigger on a regular function with noexcept(false)

https://godbolt.org/z/j3ac791qo

Is it a bug on the AST side?

@vbvictor
Copy link
Contributor

vbvictor commented Mar 24, 2025

I tried to reproduce the issue in a simplified clang-query request. Does anybody can explain why the simple matcher functionDecl(isNoThrow()) triggers on lambda like []() noexcept(false) { throw 0; }; ? While it doesn't trigger on a regular function with noexcept(false)

In your example, only CXXConversionDecl and CXXDestructorDecl are matched as noexcept functions- https://godbolt.org/z/aefG99dYv. The main operator() with throw expression is not matched.

Your misunderstanding happens because ctor/dtor/operator() of lambda points to one location.

@vbvictor
Copy link
Contributor

vbvictor commented Mar 24, 2025

Note: code example in issue is only false-positive when lambda is not assigned to a variable.

void awoo() noexcept { // no warning - lambda assigned to a variable and never called - GOOD
    auto l = [] { throw 0; };
}

void awoo() noexcept { // warning - lambda is called - GOOD
    auto l = [] { throw 0; };
    l();
}

void awoo() noexcept { // warning - lambda is not assigned to a variable and never called - BAD
    [] { throw 0; };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-tidy false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

5 participants