You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix multiple issues with warning wave docs (#46487)
Fixes#29521Fixes#33559Fixes#37157Fixes#45181Fixes#45490
Many of these are duplicates regarding the changes in the behavior of AnalysisLevel and WarningLevel introduced in .NET 7. Other changes are commas vs. semicolons in lists of errors and the csc command line argument.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/warning-waves.md
+14-6Lines changed: 14 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Compiler warning waves"
3
-
description: "C# warning waves are optional warnings that can be reported on code where previously a warning wouldn't have been reported. They represent practices that could be harmful, or potentially elements that might be breaking changes in the future."
4
-
ms.date: 07/01/2024
3
+
description: "C# warning waves are optional warnings that can be reported on code where previously a warning isn't reported. They represent practices that could be harmful, or potentially elements that might be breaking changes in the future."
4
+
ms.date: 05/29/2025
5
5
f1_keywords:
6
6
- "CS7023"
7
7
- "CS8073"
@@ -39,7 +39,17 @@ helpviewer_keywords:
39
39
---
40
40
# C# Warning waves
41
41
42
-
New warnings and errors can be introduced in each release of the C# compiler. When new warnings could be reported on existing code, those warnings are introduced under an opt-in system referred to as a *warning wave*. The opt-in system means that you shouldn't see new warnings on existing code without taking action to enable them. Warning waves are enabled using the [**AnalysisLevel**](../compiler-options/errors-warnings.md#analysis-level) element in your project file. When `<TreatWarningsAsErrors>true</TreatWarningsAsErrors>` is specified, enabled warning wave warnings generate errors. Warning wave 5 diagnostics were added in C# 9. Warning wave 6 diagnostics were added in C# 10. Warning wave 7 diagnostics were added in C# 11. Warning wave 8 diagnostics were added in C# 12.
42
+
New warnings and errors can be introduced in each release of the C# compiler. When new warnings could be reported on existing code, those warnings are introduced under an opt-in system referred to as a *warning wave*. The opt-in system means that you shouldn't see new warnings on existing code without taking action to enable them. When `<TreatWarningsAsErrors>true</TreatWarningsAsErrors>` is specified, enabled warning wave warnings generate errors. Warning wave 5 diagnostics were added in C# 9. Warning wave 6 diagnostics were added in C# 10. Warning wave 7 diagnostics were added in C# 11. Warning wave 8 diagnostics were added in C# 12. Warning wave 9 diagnostics were added in C# 13.
43
+
44
+
Beginning with the .NET 7 SDK (C# 11), the build system sets warning waves with the following rules:
45
+
46
+
- AnalysisLevel tracks the current TFM if not specified
47
+
- AnalysisLevel is set to latest if the current TFM is the 'latest' TFM (as defined by a property that we need to bump)
48
+
- WarningLevel should track the current TFM if not specified
49
+
- WarningLevel shouldn't override the user-provided value
50
+
- WarningLevel should be set to 4 if the project is a .NET Framework project
51
+
52
+
For SDKs earlier than .NET 7, AnalysisLevel always overwrote WarningLevel.
43
53
44
54
## CS9123 - Taking address of local or parameter in async method can create a GC hole.
45
55
@@ -50,8 +60,6 @@ The following code produces CS9123:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-options/errors-warnings.md
+24-17Lines changed: 24 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
description: "C# Compiler Options for errors and warnings. These options suppress or enable warnings, and control warnings as errors."
3
3
title: "Compiler Options - errors and warnings"
4
-
ms.date: 10/30/2023
4
+
ms.date: 05/30/2025
5
5
f1_keywords:
6
6
- "cs.build.options"
7
7
helpviewer_keywords:
@@ -23,15 +23,15 @@ The following options control how the compiler reports errors and warnings.
23
23
|`WarningLevel`|`-warn`| Set warning level. [More info.](#warninglevel)|
24
24
|`AnalysisLevel`| / | Set optional warning level. [More info.](#analysis-level)|
25
25
|`TreatWarningsAsErrors`|`-warnaserror`| Treat all warnings as errors. [More info.](#treatwarningsaserrors)|
26
-
|`WarningsAsErrors`|`-warnaserror`| Treat one or more warnings as errors. [More info.](#warningsaserrors-and-warningsnotaserrors)|
27
-
|`WarningsNotAsErrors`|`-warnnotaserror`| Treat one or more warnings not as errors. [More info.](#warningsaserrors-and-warningsnotaserrors)|
26
+
|`WarningsAsErrors`|`-warnaserror+`| Treat one or more warnings as errors. [More info.](#warningsaserrors-and-warningsnotaserrors)|
27
+
|`WarningsNotAsErrors`|`-warnaserror-`| Treat one or more warnings not as errors. [More info.](#warningsaserrors-and-warningsnotaserrors)|
28
28
|`NoWarn`|`-nowarn`| Set a list of disabled warnings. [More info.](#nowarn)|
29
29
|`CodeAnalysisRuleSet`|`-ruleset`| Specify a ruleset file that disables specific diagnostics. [More info.](#codeanalysisruleset)|
30
30
|`ErrorLog`|`-errorlog`| Specify a file to log all compiler and analyzer diagnostics. [More info.](#errorlog)|
31
-
|`ReportAnalyzer`|`-reportanalyzer`| Report additional analyzer information, such as execution time. [More info.](#reportanalyzer)|
31
+
|`ReportAnalyzer`|`-reportanalyzer`| Report extra analyzer information, such as execution time. [More info.](#reportanalyzer)|
32
32
33
33
> [!NOTE]
34
-
> Refer to [Compiler options](index.md#how-to-set-options) for more information on configuring these options for your project.
34
+
> For more information about configuring these options for your project, see the [Compiler options](index.md#how-to-set-options). For more information on the available switches, see the [MSBuild command line switches](/visualstudio/msbuild/msbuild-command-line-reference#switches) article in the Visual Studio documentation.
35
35
36
36
## WarningLevel
37
37
@@ -52,13 +52,21 @@ The element value is the warning level you want displayed for the compilation: L
52
52
| 4 (default) | Displays all level 3 warnings plus informational warnings. |
53
53
54
54
> [!WARNING]
55
-
> The compiler command line accepts values greater than 4 to enable [warning wave warnings](../compiler-messages/warning-waves.md). However, the .NET SDK sets the *WarningLevel* to match the *AnalysisLevel* in your project file.
55
+
> The compiler command line accepts values greater than 4 to enable [warning wave warnings](../compiler-messages/warning-waves.md).
56
56
57
57
To get information about an error or warning, you can look up the error code in the [Help Index](/visualstudio/help-viewer/install-manage-local-content). For other ways to get information about an error or warning, see [C# Compiler Errors](../compiler-messages/index.md). Use [**TreatWarningsAsErrors**](#treatwarningsaserrors) to treat all warnings as errors. Use [**DisabledWarnings**](#nowarn) to disable certain warnings.
58
58
59
59
## Analysis level
60
60
61
-
The **AnalysisLevel** option specifies additional [warning waves](../compiler-messages/warning-waves.md) and analyzers to enable. Warning wave warnings are additional checks that improve your code, or ensure it will be compatible with upcoming releases. Analyzers provide lint-like capability to improve your code.
61
+
The **AnalysisLevel** option specifies higher [warning waves](../compiler-messages/warning-waves.md) and analyzers to enable in .NET 7 and later projects. Warning wave warnings are extra checks that improve your code, or ensure it remains compatible with upcoming releases. Analyzers provide lint-like capability to improve your code.
62
+
63
+
Beginning with the .NET 7 SDK, **AnalysisLevel** and **WarningLevel** are set based on these rules:
64
+
65
+
- The default **AnalysisLevel** matches the Target Framework Moniker (TFM) from the project file.
66
+
- The default **WarningLevel** matches the value for **AnalysisLevel**.
67
+
- The default **WarningLevel** is 4 for .NET Framework projects.
68
+
69
+
Before the .NET 7 SDK, the **AnalysisLevel** overwrote the **WarningLevel**.
62
70
63
71
```xml
64
72
<AnalysisLevel>preview</AnalysisLevel>
@@ -85,7 +93,7 @@ The **TreatWarningsAsErrors** option treats all warnings as errors. You can also
All warning messages are instead reported as errors. The build process halts (no output files are built). By default, **TreatWarningsAsErrors** isn't in effect, which means warnings don't prevent the generation of an output file. Optionally, if you want only a few specific warnings to be treated as errors, you may specify a comma-separated list of warning numbers to treat as errors. The set of all nullability warnings can be specified with the [**Nullable**](language.md#nullable) shorthand. Use [**WarningLevel**](#warninglevel) to specify the level of warnings that you want the compiler to display. Use [**NoWarn**](#nowarn) to disable certain warnings.
96
+
All warning messages are instead reported as errors. The build process halts (no output files are built). By default, **TreatWarningsAsErrors** isn't in effect, which means warnings don't prevent the generation of an output file. Optionally, if you want only a few specific warnings to be treated as errors, you can specify a comma-separated list of warning numbers to treat as errors. The set of all nullability warnings can be specified with the [**Nullable**](language.md#nullable) shorthand. Use [**WarningLevel**](#warninglevel) to specify the level of warnings that you want the compiler to display. Use [**NoWarn**](#nowarn) to disable certain warnings.
89
97
90
98
> [!IMPORTANT]
91
99
> There are two subtle differences between using the `<TreatWarningsAsErrors>` element in your *csproj* file, and using the `warnaserror` MSBuild command line switch. *TreatWarningsAsErrors* only impacts the C# compiler, not any other MSBuild tasks in your *csproj* file. The `warnaserror` command line switch impacts all tasks. Secondly, the compiler doesn't produce any output on any warnings when *TreatWarningsAsErrors* is used. The compiler produces output when the `warnaserror` command line switch is used.
@@ -97,16 +105,16 @@ The **WarningsAsErrors** and **WarningsNotAsErrors** options override the **Trea
97
105
Enable warnings 0219, 0168, and all nullable warnings as errors:
You use **WarningsAsErrors** to configure a set of warnings as errors. Use **WarningsNotAsErrors** to configure a set of warnings that should not be errors when you've set all warnings as errors.
117
+
You use **WarningsAsErrors** to configure a set of warnings as errors. Use **WarningsNotAsErrors** to configure a set of warnings that shouldn't be errors when you set all warnings as errors.
110
118
111
119
## NoWarn
112
120
@@ -116,20 +124,19 @@ The **NoWarn** option lets you suppress the compiler from displaying one or more
116
124
<NoWarn>warningnumber1,warningnumber2</NoWarn>
117
125
```
118
126
119
-
You need to specify only the numeric part of the warning identifier. For example, if you want to suppress *CS0028*, you could specify `<NoWarn>28</NoWarn>`. The compiler silently ignores warning numbers passed to **NoWarn** that were valid in previous releases, but that have been removed. For example, *CS0679* was valid in the compiler in Visual Studio .NET 2002 but was removed later.
127
+
You need to specify only the numeric part of the warning identifier. For example, if you want to suppress *CS0028*, you could specify `<NoWarn>28</NoWarn>`. The compiler silently ignores warning numbers passed to **NoWarn** that were valid in previous releases, but aren't generated by the current compiler. For example, *CS0679* was valid in the compiler in Visual Studio .NET 2002 but was removed later.
120
128
121
129
The following warnings can't be suppressed by the **NoWarn** option:
122
130
123
131
- Compiler Warning (level 1) CS2002
124
132
- Compiler Warning (level 1) CS2023
125
133
- Compiler Warning (level 1) CS2029
126
134
127
-
Note that warnings are intended to be an indication of a potential problem with your code, so you should understand the risks of disabling any particular warning. Use **NoWarn** only when you're certain that a warning is a false positive and can't possibly be a runtime bug.
135
+
Warnings are intended to be an indication of a potential problem with your code, so you should understand the risks of disabling any particular warning. Use **NoWarn** only when you're certain that a warning is a false positive and can't possibly be a runtime bug.
128
136
129
137
You might want to use a more targeted approach to disabling warnings:
130
138
131
139
- Most compilers provide ways to disable warnings just for certain lines of code, so that you can still review the warnings if they occur elsewhere in the same project. To suppress a warning only in a specific part of the code in C#, use [#pragma warning](../preprocessor-directives.md#pragma-warning).
132
-
133
140
- If your goal is to see more concise and focused output in your build log, you might want to change the build log verbosity. For more information, see [How to: View, save, and configure build log files](/visualstudio/ide/how-to-view-save-and-configure-build-log-files).
134
141
135
142
To add warning numbers to any previously set value for **NoWarn** without overwriting it, reference `$(NoWarn)` as shown in the following example:
@@ -156,7 +163,7 @@ Specify a file to log all compiler and analyzer diagnostics.
156
163
<ErrorLog>compiler-diagnostics.sarif</ErrorLog>
157
164
```
158
165
159
-
The **ErrorLog** option causes the compiler to output a [Static Analysis Results Interchange Format (SARIF) log](https://github.com/microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md#:~:text=What%20is%20SARIF%3F,for%20use%20by%20simpler%20tools). SARIF logs are typically read by tools that analyze the results from compiler and analyzer diagnostics.
166
+
The **ErrorLog** option causes the compiler to output a [Static Analysis Results Interchange Format (SARIF) log](https://github.com/microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md#:~:text=What%20is%20SARIF%3F,for%20use%20by%20simpler%20tools). Tools that analyze compiler and analyzer results read SARIF logs.
160
167
161
168
You can specify the SARIF format using the `version` argument to the `ErrorLog` element:
162
169
@@ -168,7 +175,7 @@ The separator can be either a comma (`,`) or a semicolon (`;`). Valid values for
168
175
169
176
## ReportAnalyzer
170
177
171
-
Report additional analyzer information, such as execution time.
178
+
Report extra analyzer information, such as execution time.
172
179
173
180
```xml
174
181
<ReportAnalyzer>true</ReportAnalyzer>
@@ -177,4 +184,4 @@ Report additional analyzer information, such as execution time.
177
184
The **ReportAnalyzer** option causes the compiler to emit extra MSBuild log information that details the performance characteristics of analyzers in the build. It's typically used by analyzer authors as part of validating the analyzer.
178
185
179
186
> [!IMPORTANT]
180
-
> The extra log information generated by this flag is only generated when the `-verbosity:detailed` command line option is used. See the [switches](/visualstudio/msbuild/msbuild-command-line-reference#switches) article in the MSBuild documentation for more information.
187
+
> The extra log information generated by this flag is only generated when the `-verbosity:detailed` command line option is used. For more information, see the [switches](/visualstudio/msbuild/msbuild-command-line-reference#switches) article in the MSBuild documentation.
0 commit comments