Issue 240000
Summary [clang-tidy] performance-use-std-move segfault on copy-assignment of imported derived record (llvm_unreachable fall-through)
Labels clang-tidy
Assignees
Reporter Anthony-Gaudino
    `performance-use-std-move` segfaults while matching a plain copy-assignment whose left-hand side has a deserialized class type with a non-trivial implicit move assignment. The move operator is absent from the deserialized `methods()` list, so the checker's `hasAccessibleNonTrivialMoveAssignment` matcher falls through to `llvm_unreachable`, which crashes release builds.

This is distinct from #186789 (forward declaration with no definition, fixed by #186704): I verified that reproducer still warns correctly on this toolchain, and every type here is fully defined.

## Version

```
$ clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 23.1.1
  Optimized build.
```

Ubuntu, `x86_64-pc-linux-gnu`, `clang++` from the same toolchain. Deterministic: 3/3 runs segfault.

## Reproducer (3 small files, no external dependencies)

`types.cppm`:

```cpp
export module types;
export namespace repro {
struct SP {
  unsigned long long h = 0;
  SP& operator=(const SP& o) { h = o.h; return *this; }
  SP& operator=(SP&& o) { h = o.h; return *this; }
};
struct B1 {
  SP p;
};
struct D1 : public B1 {
};
}
```

`holder.cppm`:

```cpp
module;
#include <vector>
export module holder;
import types;
export namespace holder {
struct H1 {
  repro::D1 t;
  std::vector<int> v;
};
}
```

`tu.cpp`:

```cpp
module holder;
import types;
void f(repro::D1& d, repro::D1& s) {
  d = s;
}
```

Build and run (all in one directory):

```sh
FLAGS="-std=c++26"
clang++ $FLAGS --precompile types.cppm -o types.pcm
clang++ $FLAGS --precompile holder.cppm -o holder.pcm \
  -fmodule-file=types=./types.pcm -fprebuilt-module-path=.
clang-tidy --checks='-*,performance-use-std-move' --quiet tu.cpp -- $FLAGS \
  -fmodule-file=types=./types.pcm -fmodule-file=holder=./holder.pcm \
  -fprebuilt-module-path=.
```

## Observed

Segmentation fault (exit 139), empty stdout. stderr ends with:

```text
2.	ASTMatcher: Matching 'performance-use-std-move' against:
	CXXOperatorCallExpr : <tu.cpp:4:3, col:7>
 #0 0x00007f1a65c2a460 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/libLLVM.so.23.1+0x8a2a460)
 #1 0x00007f1a65c2aeea (/usr/lib/libLLVM.so.23.1+0x8a2aeea)
 #2 0x00007f1a5ca44cb0 (/usr/lib/libc.so.6+0x44cb0)
 #3 0x0000559bf1a6a3b5 (/usr/bin/clang-tidy+0x40c3b5)
 #4 0x00007f1a686081b5 clang::ast_matchers::internal::DynTypedMatcher::matches(...) const (/usr/lib/libclang-cpp.so.23.1+0x1a081b5)
 #5 0x0000559bf193b777 (/usr/bin/clang-tidy+0x2dd777)
 ...
```

Same `DynTypedMatcher::matches` shape as #186789 (whose stack named `hasArgument0Matcher<CXXOperatorCallExpr>` + `hasType1Matcher`).

## Expected

No crash — either a `std::move` suggestion or silence.

## Suspected cause

`UseStdMoveCheck.cpp`:

```cpp
AST_MATCHER(CXXRecordDecl, hasAccessibleNonTrivialMoveAssignment) {
  const CXXRecordDecl *ND = Node.getDefinition();
  if (!ND)
    return false;
  if (!ND->hasNonTrivialMoveAssignment())
    return false;
  for (const CXXMethodDecl *CM : ND->methods())
    if (CM->isMoveAssignmentOperator())
      return !CM->isDeleted() && CM->getAccess() == AS_public;
  llvm_unreachable("Move Assignment Operator Not Found");
}
```

#186704 fixed the `getDefinition() == nullptr` case. This looks like a second hole on the same path: when the definition exists and reports a non-trivial move assignment, but no move-assignment operator is reachable through `methods()`, control falls off the loop into `llvm_unreachable`.

Evidence from `clang-query` on the crashing TU (full AST): the imported record lists only its non-special member function — zero implicit declarations — while the `d = s` call demonstrably resolved to an implicit copy-assignment callee. A locally-defined equivalent gets its implicit move assignment materialized in the consumer (query finds it) and does not crash; neither does the same assignment in a plain (non-module) TU. The holder struct must sit in an imported interface together with the `std::vector` member — dropping either member, or defining the holder locally, makes the crash go away, which is how the trigger was narrowed down.

## Variants that do *not* crash

- Same code in a plain TU (`import` only, no `module` declaration).
- Holder struct defined locally instead of imported.
- Holder without the `vector` member, or without the record member.
- Assigning the base class or the member struct directly instead of the derived class.
- The #186704 forward-declaration pattern (`*this = src;` on an incomplete-then-defined type): correctly warns, no crash — so that fix is present and this is new.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to