Issue 86714
Summary [clang-tidy] Create check for inconsistent/missed typedef/using alias type names
Labels check-request
Assignees
Reporter FruitClover
    Consider the following example: https://godbolt.org/z/jE8Yc7sz6
```c++
using Type = int;

struct Base {
  virtual Type fooType(Type x);
 virtual int fooInt(int x);
};

struct Derived : Base {
  int fooType(int x);
  Type fooInt(Type x);
};
```

Here we misuse/miss consistent **alias names** in derived methods (compared to base
class signatures), which could lead to tricky bugs (after `using Type = ...`
changes) and this is also complicates plain text search.

The idea is that we could detect such **`using`/`typedef`** inconsistency/misuse in
some cases with clang-tidy.

To start up, we could focus on:

- Class methods only.
- Return type check.
- Argument type check.


To illustrate which cases are compatible/which are not - check the following table:
<details>
<summary>legend</summary>

- `Base Type`: return/argument type for class class methods.
- `Derived Type`: return/argument type for derived class methods.
- `Warning/FixItHint`: if check should emit warning/hint 
   
(we use `int` as some non-aliased type, and <alias> as `using T = ..` or
`typedef .. T`;
</details>

```
|     | Base Type | Derived Type | Warning | FixItHint |
|-----+-----------+--------------+---------+-----------|
|     | int | int          | -       | -         |
|     | <alias>   | int | +       | +         |
|     | int       | <alias>      | +       | - |
| (*) | <alias>   | <alias>      | Analyze | - |
```

`(*)` **Both types are aliased**: Analyze `BaseT` vs `DerivedT`:

1. Alias names are compatible: (**Warning: No**)
1.1 one of (`BaseT`, `DerivedT`) is implicit
2.1 Same fully-qualified names:`BaseT.getQualifiedNameAsString == DerivedT.getQualifiedNameAsString`


2. Alias names are non-compatible: (**Warning: Yes**)
2.1. Strict check: `TD1.getQualifiedNameAsString != TD2.getQualifiedNameAsString`  (`N1::Type vs N2::Type`)

3. Questinable: (**Warning: ?**)
3.1. chain alias (maybe control with option). If so, should check it before 2.1
```c++
using T1 = int;
using T2 = T1;

// => T1 name is compatible with T2 name
```

This probably belongs to `bugprone` category, something like `inconsistant-alias-names`/`inconsistant-using-typedef` (suggestions are welcome)

Background: PoC version was prepared for internal projects, here I want to check if clang-tidy would be interested in it before publishing PR, and discuss what to warn and if we should emit more fix hints.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to