Skip to content

Commit 161d5e8

Browse files
authored
Add missing code analysis warning text (#4566)
1 parent dfd7acc commit 161d5e8

36 files changed

+242
-83
lines changed

docs/code-quality/c26400.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ helpviewer_keywords: ["C26400"]
77
---
88
# Warning C26400
99

10-
This check helps to enforce the *rule I.11: Never transfer ownership by a raw pointer (T\*)*, which is a subset of the rule *R.3: A raw pointer (a T\*) is non-owning*. Specifically, it warns on any call to `operator new`, which saves its result in a variable of raw pointer type. It also warns on calls to functions that return `gsl::owner<T>` if their results are assigned to raw pointers. The idea is that you should clearly state ownership of memory resources. For more information, see the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management).
10+
> Do not assign the result of an allocation or a function call with an `owner<T>` return value to a raw pointer, use `owner<T>` instead (i.11)
11+
12+
## Remarks
13+
14+
This check helps to enforce the *rule I.11: Never transfer ownership by a raw pointer (T\*), which is a subset of the rule *R.3: A raw pointer (a T\*) is non-owning*. Specifically, it warns on any call to `operator new`, which saves its result in a variable of raw pointer type. It also warns on calls to functions that return `gsl::owner<T>` if their results are assigned to raw pointers. The idea is that you should clearly state ownership of memory resources. For more information, see the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management).
1115

1216
The easiest way to fix this warning is to use **`auto`** declaration if the resource is assigned immediately at the variable declaration. If this fix isn't possible, then we suggest that you use the type `gsl::owner<T>`. The **`auto`** declarations initialized with operator **`new`** are "owners" because we assume that the result of any allocation is implicitly an owner pointer. We transfer this assumption to the **`auto`** variable and treat it as `owner<T>`.
1317

1418
If this check flags a call to a function that returns `owner<T>`, it may be an indication of a legitimate bug in the code. Basically, it points to a place where the code leaks an explicit notion of ownership (and maybe the resource itself).
1519

16-
## Remarks
17-
1820
This rule currently checks only local variables. If you assign an allocation to a formal parameter, global variable, class member, and so on, it's not flagged. Appropriate coverage of such scenarios is planned for future work.
1921

22+
Code analysis name: `NO_RAW_POINTER_ASSIGNMENT`
23+
2024
## Example 1: Simple allocation
2125

2226
```cpp

docs/code-quality/c26401.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ description: CppCoreCheck rule C26401 enforces C++ Core Guidelines I.11
88
---
99
# Warning C26401
1010

11+
> Do not delete a raw pointer that is not an `owner<T>` (i.11)
12+
13+
## Remarks
14+
1115
This check detects code where moving to `owner<T>` can be a good option for the first stage of refactoring. Like C26400, it enforces rules I.11 and R.3, but focuses on the "release" portion of the pointer lifetime. It warns on any call to operator **`delete`** if its target isn't an `owner<T>` or an implicitly assumed owner. For more information about **`auto`** declarations, see [C26400](c26400.md). This check includes expressions that refer to global variables, formal parameters, and so on.
1216

1317
Warnings C26400 and C26401 always occur with [C26409](c26409.md), but they're more appropriate for scenarios where immediate migration to smart pointers isn't feasible. In such cases, the `owner<T>` concept can be adopted first, and C26409 may be temporarily suppressed.
1418

19+
Code analysis name: `DONT_DELETE_NON_OWNER`
20+
1521
## See also
1622

1723
[C++ Core Guidelines I.11](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i11-never-transfer-ownership-by-a-raw-pointer-t-or-reference-t)

docs/code-quality/c26403.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ ms.assetid: 7e14868d-df86-4df3-98d3-71b1e80ba14e
88
---
99
# Warning C26403
1010

11+
> Reset or explicitly delete an `owner<T>` pointer '*variable*' (r.3)
12+
1113
Owner pointers are like unique pointers: they own a resource exclusively, and manage release of the resource, or its transfers to other owners. This check validates that a local owner pointer properly maintains its resource through all execution paths in a function. If the resource wasn't transferred to another owner, or wasn't explicitly release, the checker warns, and points to the declaration of the pointer variable.
1214

1315
For more information, see the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management).
@@ -20,7 +22,11 @@ For more information, see the [C++ Core Guidelines](https://github.com/isocpp/Cp
2022

2123
- The warning may fire on clearly false positive cases where memory is deleted only after the null check of a pointer. These false positives are the result of a current limitation of the tool's API, but it may be improved in future.
2224

23-
## Example 1: Missing cleanup during error handling
25+
Code analysis name: `RESET_OR_DELETE_OWNER`
26+
27+
## Example
28+
29+
Missing cleanup during error handling:
2430

2531
```cpp
2632
gsl::owner<int*> sequence = GetRandomSequence(); // C26403

docs/code-quality/c26404.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ ms.assetid: 94afb700-3f3b-40db-8afc-2481935360c2
88
---
99
# Warning C26404
1010

11-
Once owner pointer releases or transfers its resource, it gets into an "invalid" state.
12-
Deleting such a pointer may lead to immediate memory corruption due to double delete, or to an access violation when the deleted resource is accessed from another owner pointer.
11+
> Do not delete an `owner<T>` which may be in invalid state (r.3)
1312
14-
## Example 1: Deleting an owner after transferring its value
13+
## Remarks
14+
15+
Once an owner pointer releases or transfers its resource, it gets into an "invalid" state. Deleting such a pointer may lead to immediate memory corruption due to double delete, or to an access violation when the deleted resource is accessed from another owner pointer.
16+
17+
Code analysis name: `DONT_DELETE_INVALID`
18+
19+
## Example 1
20+
21+
Deleting an owner after transferring its value:
1522

1623
```cpp
1724
gsl::owner<State*> validState = nullptr;
@@ -21,7 +28,9 @@ if (!IsValid(state))
2128
delete state; // C26404
2229
```
2330

24-
## Example 2: Deleting an uninitialized owner
31+
## Example 2
32+
33+
Deleting an uninitialized owner:
2534

2635
```cpp
2736
gsl::owner<Message*> message;

docs/code-quality/c26405.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ ms.assetid: 2034d961-3ec5-4184-bbef-aa792e4c03c0
88
---
99
# Warning C26405
1010

11+
> Do not assign to an `owner<T>` which may be in valid state (r.3)
12+
13+
## Remarks
14+
1115
If an owner pointer already points to a valid memory buffer, it must not be assigned to another value without releasing its current resource first. Such assignment may lead to a resource leak even if the resource address is copied into some raw pointer (because raw pointers shouldn't release resources). For more information, see the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r3-a-raw-pointer-a-t-is-non-owning).
1216

13-
## Example 1: Overwriting an owner in a loop
17+
Code analysis name: `DONT_ASSIGN_TO_VALID`
18+
19+
## Example 1
20+
21+
Overwriting an owner in a loop:
1422

1523
```cpp
1624
gsl::owner<Shape*> shape = nullptr;

docs/code-quality/c26406.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ ms.assetid: 02fb8e23-1989-4e24-a5a5-e30f71d00325
88
---
99
# Warning C26406
1010

11+
> Do not assign a raw pointer to an `owner<T>` (r.3)
12+
1113
This warning enforces R.3 from the C++ Core Guidelines. For more information, see [C++ Core Guidelines R.3](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r3-a-raw-pointer-a-t-is-non-owning).
1214

1315
## Remarks
1416

1517
Owners are initialized from allocations or from other owners. This warning occurs when you assign a value from a raw pointer to an owner pointer. Raw pointers don't guarantee ownership transfer; the original owner may still hold the resource and attempt to release it. It's okay to assign a value from an owner to a raw pointer. Raw pointers are valid clients to access resources, but not to manage them.
1618

17-
## Example 1: Using address of object
19+
Code analysis name: `DONT_ASSIGN_RAW_TO_OWNER`
20+
21+
## Example
22+
23+
Using address of object:
1824

1925
This sample attempts to assign ownership of the address of `defaultSocket` to owner pointer `socket`:
2026

docs/code-quality/c26407.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ ms.assetid: 5539907a-bfa0-40db-82a6-b860c97209e1
88
---
99
# Warning C26407
1010

11+
> Prefer scoped objects, don't heap-allocate unnecessarily (r.5)
12+
1113
To avoid unnecessary use of pointers, we try to detect common patterns of local allocations. For example, we detect when the result of a call to operator **`new`** is stored in a local variable and later explicitly deleted. This check supports the [C++ Core Guidelines rule R.5](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r5-prefer-scoped-objects-dont-heap-allocate-unnecessarily): *Prefer scoped objects, don't heap-allocate unnecessarily*. To fix the issue, use an RAII type instead of a raw pointer, and allow it to deal with resources. Obviously, it isn't necessary to create a wrapper type to allocate a single object. Instead, a local variable of the object's type would work better.
1214

1315
## Remarks
@@ -20,6 +22,8 @@ To avoid unnecessary use of pointers, we try to detect common patterns of local
2022

2123
- The pattern is detected only for local variables. We don't warn in cases where an allocation is assigned to, say, a global variable and then deleted in the same function.
2224

25+
Code analysis name: `DONT_HEAP_ALLOCATE_UNNECESSARILY`
26+
2327
## Example 1: Unnecessary object allocation on heap
2428

2529
```cpp

docs/code-quality/c26408.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ description: CppCoreCheck rule that enforces C++ Core Guidelines R.10
88
---
99
# Warning C26408
1010

11+
> Avoid `malloc()` and `free()`, prefer the `nothrow` version of `new` with `delete` (r.10)
12+
1113
This warning flags places where `malloc` or `free` is invoked explicitly in accordance to R.10: Avoid `malloc` and `free`. One potential fix for such warnings would be to use [std::make_unique](../standard-library/memory-functions.md#make_unique) to avoid explicit creation and destruction of objects. If such a fix isn't acceptable, operator [new and delete](../cpp/new-and-delete-operators.md) should be preferred. In some cases, if exceptions aren't welcome, `malloc` and `free` can be replaced with the nothrow version of operators `new` and `delete`.
1214

1315
## Remarks
@@ -16,6 +18,8 @@ This warning flags places where `malloc` or `free` is invoked explicitly in acco
1618

1719
- To detect `free()`, we check global functions named `free` or `std::free` that return no result and accept one parameter, which is a pointer to **`void`**.
1820

21+
Code analysis name: `NO_MALLOC_FREE`
22+
1923
## See also
2024

2125
[C++ Core Guidelines R.10](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r10-avoid-malloc-and-free)

docs/code-quality/c26409.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.assetid: a3b3a229-d566-4be3-bd28-2876ccc8dc37
88
---
99
# Warning C26409
1010

11-
> `Avoid calling new and delete explicitly, use std::make_unique<T> instead (r.11).`
11+
> Avoid calling `new` and `delete` explicitly, use `std::make_unique<T>` instead (r.11).
1212
1313
Even if code is clean of calls to `malloc` and `free`, we still suggest that you consider better options than explicit use of operators [`new` and `delete`](../cpp/new-and-delete-operators.md).
1414

@@ -21,6 +21,8 @@ The ultimate fix is to use smart pointers and appropriate factory functions, suc
2121

2222
- The checker warns on calls to any kind of operator **`new`** or **`delete`**: scalar, vector, overloaded versions (global and class-specific), and placement versions. The placement **`new`** case may require some clarifications in the Core Guidelines for suggested fixes, and may be omitted in the future.
2323

24+
Code analysis name: `NO_NEW_DELETE`
25+
2426
## Examples
2527

2628
This example shows C26409 is raised for explicit **`new`** and **`delete`**. Consider using smart pointer factory functions such as `std::make_unique` instead.

docs/code-quality/c26410.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ ms.assetid: d1547faf-96c6-48da-90f5-841154d0e878
88
---
99
# Warning C26410
1010

11+
> The parameter '*parameter*' is a reference to const unique pointer, use `const T*` or `const T&` instead (r.32)
12+
1113
Generally, references to const unique pointer are meaningless. They can safely be replaced by a raw reference or a pointer. This warning enforces [C++ Core Guidelines rule R.32](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r32-take-a-unique_ptrwidget-parameter-to-express-that-a-function-assumes-ownership-of-a-widget).
1214

1315
## Remarks
@@ -16,7 +18,11 @@ Generally, references to const unique pointer are meaningless. They can safely b
1618

1719
- Template code may produce noisy warnings. Keep in mind that templates can be instantiated with various type parameters with different levels of indirection, including references. Some warnings may not be obvious and fixes may require some rework of templates (for example, explicit removal of reference indirection). If template code is intentionally generic, the warning can be suppressed.
1820

19-
## Example 1: Unnecessary reference
21+
Code analysis name: `NO_REF_TO_CONST_UNIQUE_PTR`
22+
23+
## Example
24+
25+
Unnecessary reference:
2026

2127
```cpp
2228
std::vector<std::unique_ptr<Tree>> roots = GetRoots();

docs/code-quality/c26411.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ ms.assetid: 5134e51e-8b92-4ee7-94c3-022e318a0e24
88
---
99
# Warning C26411
1010

11+
> The parameter '*parameter*' is a reference to unique pointer and it is never reassigned or reset, use `T*` or `T&` instead (r.33)
12+
1113
When you pass a unique pointer to a function by reference, it implies that its resource may be released or transferred inside the function. If the function uses its parameter only to access the resource, it's safe to pass a raw pointer or a reference. For more information, see [C++ Core Guidelines rule R.33](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r33-take-a-unique_ptrwidget-parameter-to-express-that-a-function-reseats-thewidget): *Take a unique_ptr\<widget\>& parameter to express that a function reseats the widget*.
1214

1315
## Remarks
@@ -20,6 +22,8 @@ When you pass a unique pointer to a function by reference, it implies that its r
2022

2123
- Lambda expressions that do implicit capture-by-reference may lead to surprising warnings about references to unique pointers. Currently, all captured reference parameters in lambdas are reported, regardless of whether they're reset or not. A future release may extend the heuristic to correlate lambda fields and lambda parameters.
2224

25+
Code analysis name: `NO_REF_TO_UNIQUE_PTR`
26+
2327
## Example: Unnecessary reference
2428

2529
```cpp

docs/code-quality/c26414.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ The type `Microsoft::WRL::ComPtr` behaves as a shared pointer, but it's often us
2727

2828
This check looks for explicit local allocations assigned to smart pointers, to identify if scoped variables could work as an alternative. Both direct calls to operator `new`, and special functions like `std::make_unique` and `std::make_shared`, are interpreted as direct allocations.
2929

30+
Code analysis name: `RESET_LOCAL_SMART_PTR`
31+
3032
## Example
3133

32-
Dynamic buffer
34+
Dynamic buffer:
3335

3436
```cpp
3537
void unpack_and_send(const frame &f)
@@ -40,7 +42,7 @@ void unpack_and_send(const frame &f)
4042
}
4143
```
4244
43-
Dynamic buffer replaced by container
45+
Dynamic buffer replaced by container:
4446
4547
```cpp
4648
void unpack_and_send(const frame &f)

docs/code-quality/c26426.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ ms.assetid: 6fb5f6d2-b097-47f8-8b49-f2fd4e9bef0e
88
---
99
# Warning C26426
1010

11-
"Global initializer calls a non-constexpr function."
11+
> Global initializer calls a non-constexpr function '*symbol*' (i.22)
12+
13+
## C++ Core Guidelines
1214

13-
**C++ Core Guidelines**:
1415
[I.22](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i22-avoid-complex-initialization-of-global-objects): Avoid complex initialization of global objects
1516

1617
The order of execution of initializers for global objects may be inconsistent or undefined, which can lead to issues that are hard to reproduce and investigate. To avoid such problems, global initializers shouldn't depend on external code that's executed at run time, and that may depend on data that's not yet initialized. This rule flags cases where global objects call functions to obtain their initial values.
@@ -25,9 +26,11 @@ The order of execution of initializers for global objects may be inconsistent or
2526

2627
- Static class members are considered global, so their initializers are also checked.
2728

29+
Code analysis name: `NO_GLOBAL_INIT_CALLS`
30+
2831
## Examples
2932

30-
external version check
33+
External version check:
3134

3235
```cpp
3336
// api.cpp
@@ -41,7 +44,7 @@ int get_api_version() noexcept;
4144
bool is_legacy_mode = get_api_version() <= API_LEGACY_VERSION; // C26426, also stale value
4245
```
4346

44-
external version check made more reliable
47+
External version check made more reliable:
4548

4649
```cpp
4750
// api.cpp

docs/code-quality/c26427.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.assetid: 8fb95a44-8704-45b1-bc55-eccd59b1db2f
88
---
99
# Warning C26427
1010

11-
"Global initializer accesses extern object."
11+
> Global initializer accesses extern object '*symbol*' (i.22)
1212
1313
**C++ Core Guidelines**:
1414
[I.22](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i22-avoid-complex-initialization-of-global-objects): Avoid complex initialization of global objects
@@ -24,9 +24,11 @@ An object is deemed **`extern`** if it conforms to the following rules:
2424
- it's not marked as **`const`**;
2525
- Static class members are considered global, so their initializers are also checked.
2626

27+
Code analysis name: `NO_GLOBAL_INIT_EXTERNS`
28+
2729
## Example
2830

29-
external version check
31+
External version check:
3032

3133
```cpp
3234
// api.cpp
@@ -37,7 +39,7 @@ extern int api_version;
3739
bool is_legacy_mode = api_version <= API_LEGACY_VERSION; // C26427, also stale value
3840
```
3941

40-
external version check made more reliable
42+
External version check made more reliable:
4143

4244
```cpp
4345
// api.cpp

docs/code-quality/c26429.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ A variable is marked as checked for null when it's used in the following context
2929

3030
The rule doesn't have full dataflow tracking. It can produce incorrect results in cases where indirect checks are used (such as when an intermediate variable holds a null value and is later used in a comparison).
3131

32+
Code analysis name: `USE_NOTNULL`
33+
3234
## Example
3335

34-
hidden expectation
36+
Hidden expectation:
3537

3638
```cpp
3739
using client_collection = gsl::span<client*>;
@@ -48,7 +50,7 @@ void keep_alive(const connection *connection) // C26429
4850
}
4951
```
5052
51-
hidden expectation clarified by gsl::not_null
53+
Hidden expectation clarified by `gsl::not_null`:
5254
5355
```cpp
5456
using client_collection = gsl::span<gsl::not_null<client*>>;

docs/code-quality/c26431.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.assetid: 40be6032-c8de-49ab-8e43-e8eedc0ca0ba
88
---
99
# Warning C26431
1010

11-
"The type of expression is already gsl::not_null. Do not test it for nullness."
11+
> The type of expression '*expr*' is already `gsl::not_null`. Do not test it for nullness (f.23)
1212
1313
**C++ Core Guidelines**:
1414
[F.23](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#f23-use-a-not_nullt-to-indicate-that-null-is-not-a-valid-value): Use a not_null\<T> to indicate that "null" isn't a valid value
@@ -25,6 +25,8 @@ The current heuristic for null checks detects the following contexts:
2525
- non-bitwise logical operations;
2626
- comparison operations where one operand is a constant expression that evaluates to zero.
2727

28+
Code analysis name: `DONT_TEST_NOTNULL`
29+
2830
## Example
2931

3032
Unnecessary null checks reveal questionable logic:
@@ -58,7 +60,7 @@ public:
5860
};
5961
```
6062

61-
Unnecessary null checks reveal questionable logic - reworked:
63+
Unnecessary null checks reveal questionable logic, reworked:
6264

6365
```cpp
6466
//...

docs/code-quality/c26435.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ helpviewer_keywords: ["C26435"]
77
---
88
# Warning C26435
99

10-
"Function should specify exactly one of 'virtual', 'override', or 'final'."
10+
> Function '*symbol*' should specify exactly one of 'virtual', 'override', or 'final' (c.128)
1111
1212
## C++ Core Guidelines
1313

0 commit comments

Comments
 (0)