https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122105

            Bug ID: 122105
           Summary: Add fix-it hint for -Waddress warning about lambda
                    that isn't invoked
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

int main()
{
  static_assert([] { return true; });  // oops
}


We get a correct warning here:

lambda.cc: In function 'int main()':
lambda.cc:3:17: warning: the address of 'static constexpr bool
main()::<lambda()>::_FUN()' will never be NULL [-Waddress]
    3 |   static_assert([] { return true; });  // oops
      |                 ^~~~~~~~~~~~~~~~~~~
lambda.cc:3:17: note: 'static constexpr bool main()::<lambda()>::_FUN()'
declared here
    3 |   static_assert([] { return true; });  // oops
      |                 ^


But it takes a little bit of figuring out to realize what it's saying, and that
the lambda expression is being implicitly converted to a non-null function
pointer.

It would be more helpful to add a fix-it:

note: maybe you meant to immediately invoke the lambda expression?
  static_assert([] { return true; });  // oops
                                   ^
                                   ()

The fix-it is only valid if the lambda expression is callable with no
arguments. If it requires one or more arguments we could still give the note,
but not the fixit showing the '()' insertion (because we don't know what
arguments the user intended to use there). In practice, I think it's uncommon
to forget to invoke a lambda expression that needs arguments, so the major
benefit will be for cases where () is the right fix.

It would also be nice to stop showing the 'main()::<lambda()>::_FUN'
implementation details, by detecting this case and give a custom diagnostic,
something like "converting a lambda expression to a function pointer will never
be null". With the "maybe you meant ..." note suggested above I think it would
be a nice diagnostic.

Reply via email to