Skip to content

hicpp-use-nullptr does not make sense with spaceship operator #45580

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

Closed
pavelkryukov opened this issue Jun 7, 2020 · 9 comments
Closed

hicpp-use-nullptr does not make sense with spaceship operator #45580

pavelkryukov opened this issue Jun 7, 2020 · 9 comments
Labels
bugzilla Issues migrated from bugzilla clang-tidy

Comments

@pavelkryukov
Copy link

Bugzilla Link 46235
Resolution FIXED
Resolved on Feb 09, 2021 14:27
Version unspecified
OS All
CC @dermojo,@maxim-banaev
Fixed by commit(s) 98146c1

Extended Description

Hello

With the following input:

#include

struct A {
int a;
constexpr auto operator<=>(const A&) const = default;
};

bool foo(A x, A y) { return x < y; }

and command line:

clang-tidy-10 -checks='hicpp-use-nullptr' tidy20.cpp -- -std=c++20

Clang-Tidy generates following output which does not make sense:

./tidy20.cpp:5:56: warning: use nullptr [hicpp-use-nullptr]
constexpr auto operator<=>(const A&) const = default;
^
nullptr
./tidy20.cpp:8:31: warning: use nullptr [hicpp-use-nullptr]
bool foo(A x, A y) { return x < y; }
^
nullptr

@pavelkryukov
Copy link
Author

godbolt.org: https://godbolt.org/z/3Ca-Zn

@llvmbot
Copy link
Member

llvmbot commented Jun 8, 2020

Looking at the AST it's fairly easy to see why this warning is generated

In the defaulted function definition:

| | | | -ImplicitCastExpr <col:56> 'std::__1::_CmpUnspecifiedParam':'void (std::__1::_CmpUnspecifiedType::*)()' <NullToMemberPointer> | | | | -IntegerLiteral col:56 'int' 0

In foo(A, A):

-ImplicitCastExpr <col:31> 'std::__1::_CmpUnspecifiedParam':'void (std::__1::_CmpUnspecifiedType::*)()' <NullToMemberPointer> -IntegerLiteral col:31 'int' 0

Basically part of the spaceship operator comparison expands to

if (R cmp = static_cast(op); cmp != 0) return cmp;

It seems that the 0 is being cast to a member pointer for the comparison which ultimately results in this warning.

@llvmbot
Copy link
Member

llvmbot commented Jul 8, 2020

*** Bug llvm/llvm-bugzilla-archive#46636 has been marked as a duplicate of this bug. ***

@llvmbot
Copy link
Member

llvmbot commented Feb 8, 2021

*** Bug llvm/llvm-bugzilla-archive#49089 has been marked as a duplicate of this bug. ***

@llvmbot
Copy link
Member

llvmbot commented Feb 9, 2021

Should be fixed in 98146c1

@pavelkryukov
Copy link
Author

Thank you!

@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2021

mentioned in issue llvm/llvm-bugzilla-archive#46636

@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2021

mentioned in issue llvm/llvm-bugzilla-archive#49089

@llvmbot
Copy link
Member

llvmbot commented Nov 27, 2021

mentioned in issue llvm/llvm-bugzilla-archive#50582

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang-tidy
Projects
None yet
Development

No branches or pull requests

2 participants