File: cstyleCast.md

package info (click to toggle)
cppcheck 2.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,688 kB
  • sloc: cpp: 272,455; python: 22,408; ansic: 8,088; sh: 1,059; makefile: 1,041; xml: 987; cs: 291
file content (54 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

# cstyleCast

**Message**: C-style pointer casting<br/>
**Category**: Modernization<br/>
**Severity**: Style<br/>
**Language**: C++, not applicable for C code

## Description

Many developers feel that it's best to replace C casts with C++ casts.

There are several advantages with C++ casts:
 * they permit only certain conversions
 * they express the intent
 * they are easy to identify

This checker is about C casts that converts to/from a pointer or reference.

**Note:** More "noisy" warnings exists that warn about all C casts. For instance Clang has
`-Wold-style-cast` and there is also such checking in Misra C++.

Dangerous conversions are covered by other warnings so this ID `cstyleCast` is primarily about
writing warnings for casts that are currently safe.

# Motivation

The motivation of this checker is to modernize c++ code.

## How to fix

You can use C++ casts such as `static_cast` to fix these warnings.

The `dynamic_cast` should rarelly be used to fix these warnings because dangerousTypeCast is
reported when that can be a good idea.

Before:
```cpp
struct Base{};
struct Derived: public Base {};
void foo(Base* base) {
    Base *p = (Base*)derived; // <- cstyleCast, cast from derived object to base object is safe now
}
```

After:
```cpp
struct Base{};
struct Derived: public Base {};
void foo(Base* base) {
    Derived *p = static_cast<Derived*>(base);
}
```
The `static_cast` ensures that there will not be loss of constness in the future.