<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64201>64201</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang-tidy segfaults on bugprone-unchecked-optional-access - observable from llvmorg-16.x.x to current main
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          combinatorylogic
      </td>
    </tr>
</table>

<pre>
    Code to reproduce (as `clang-tidy test.cpp`):

```C++
#include <optional>

auto computeSomeStuff(double arg1) -> double;

class C1 {
  void estimateWhatever() const;
};

struct S1 {
  double a;
  double b;
};
  
auto C1::estimateWhatever() const -> void {
  for (int i = 0; i<1; i++) {
    std::optional<std::pair<S1, int>> result{};
 /*
      Do something forgetful.
     */
    auto& [cTf, errorcore] = *result;
    cTf.b = computeSomeStuff(cTf.a);
 }
}
```

With the following in `.clang-tidy`:

```
Checks: '
 bugprone-*
        '
```

Cause of segfault:

attempt to cast `ScalarStorageLocation` to `AggregateStorageLocation`.

Proposed fix:

test if `Loc->getKind() != StorageLocation::Kind::Aggregate` and just return nullptr if so.

[clang-bug-aggregatestoragelocation.tar.gz](https://github.com/llvm/llvm-project/files/12195811/clang-bug-aggregatestoragelocation.tar.gz)



</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVMFu4zYQ_ZrxZWBBpGzZPuiQ2PGleyjgBfZMUSOZW5oUyGE26dcXlONYSdOiBQRZHg3fezPzNCpGMziiBtaPsD4sVOKzD432l9Y4xT68Wj8YvWh999rsfUfIHgONwXdJE4LcqohQl9oqNyzZdK_IFLnQ4wh1CXIH1QOUByhv97q8XnuQj_m6RmVlnLapI4Rq70c23ikL1dP8qErsUfvLmJhO_kInTn0Pctv51FpCFQYBcodLqJ7wGoPqcQ6grYoR9wJh8xZHfPamQ4psLorpx1kxPVMAuc1I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