Skip to content

Commit 18acc93

Browse files
Merge pull request #5534 from TylerMSFT/warn
fix language tagging and some of the code samples
2 parents ef96743 + 9ea858d commit 18acc93

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

docs/code-quality/c6031.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
22
title: Warning C6031
33
description: "Describes C++ Code Analysis warning C6031 and how to resolve it."
4-
ms.date: 10/04/2022
4+
ms.date: 4/5/2024
55
f1_keywords: ["C6031", "RETVAL_IGNORED_FUNC_COULD_FAIL", "__WARNING_RETVAL_IGNORED_FUNC_COULD_FAIL"]
66
helpviewer_keywords: ["C6031"]
7-
ms.assetid: 59e1ef0a-b3ca-4ffa-bcb3-ad2bd22ece22
87
---
98
# Warning C6031
109

@@ -16,44 +15,48 @@ Warning C6031 indicates the caller doesn't check a function's return value for f
1615

1716
In general, it isn't safe to assume that calls to functions requiring disk, network, memory, or other resources will succeed. The caller should always check the return value and handle error cases appropriately. Also consider using the `_Must_inspect_result_` annotation, which checks that the value is examined in a useful way.
1817

18+
This warning applies to both C and C++ code.
19+
1920
Code analysis name: `RETVAL_IGNORED_FUNC_COULD_FAIL`
2021

2122
## Example
2223

2324
The following code generates warning C6031:
2425

25-
```cpp
26+
```c
2627
#include <stdio.h>
27-
void f( )
28+
int main()
2829
{
29-
fopen( "test.c", "r" ); // C4996, C6031 return value ignored
30+
fopen("test.c", "r"); // C4996, C6031 return value ignored
3031
// code ...
3132
}
3233
```
3334

3435
To correct this warning, check the return value of the function as shown in the following code:
3536

36-
```cpp
37+
```c
3738
#include <stdio.h>
38-
void f( )
39+
int main()
3940
{
40-
FILE *stream;
41-
if ( (stream = fopen( "test.c", "r" )) == NULL )
41+
FILE* stream;
42+
if ((stream = fopen("test.c", "r")) == NULL)
43+
{
4244
return;
45+
}
4346
// code ...
4447
}
4548
```
4649

4750
The following code uses safe function `fopen_s` to correct this warning:
4851

49-
```cpp
52+
```c
5053
#include <stdio.h>
51-
void f( )
54+
int main()
5255
{
53-
FILE *stream;
56+
FILE* stream;
5457
errno_t err;
5558

56-
if ( (err = fopen_s( &stream, "test.c", "r" )) !=0 )
59+
if ((err = fopen_s(&stream, "test.c", "r")) != 0)
5760
{
5861
// code ...
5962
}
@@ -64,23 +67,30 @@ This warning is also generated if the caller ignores the return value of a funct
6467

6568
```cpp
6669
#include <sal.h>
67-
_Check_return_ bool func();
70+
_Check_return_ bool func()
71+
{
72+
return true;
73+
}
6874

69-
void test_f()
75+
int main()
7076
{
71-
func(); // Warning C6031
77+
func();
7278
}
7379
```
7480

7581
To correct the previous warning, check the return value as shown in the following code:
7682

7783
```cpp
7884
#include <sal.h>
79-
_Check_return_ bool func();
85+
_Check_return_ bool func()
86+
{
87+
return true;
88+
}
8089

81-
void test_f()
90+
int main()
8291
{
83-
if ( func() ) {
92+
if (func())
93+
{
8494
// code ...
8595
}
8696
}
@@ -91,10 +101,12 @@ In cases where it's necessary to ignore the return value of a function, assign t
91101
```cpp
92102
#include <tuple>
93103
#include <ctime>
104+
#include <cstdlib>
94105
#include <stdio.h>
95-
void f()
106+
107+
int main()
96108
{
97-
std::srand(static_cast(std::time(nullptr))); // set initial seed value to system clock
109+
std::srand(static_cast<unsigned int>(std::time(nullptr))); // set initial seed value to system clock
98110
std::ignore = std::rand(); // Discard the first result as the few random results are always small.
99111
// ...
100112
}

0 commit comments

Comments
 (0)