Warnings as errors
A warning in a clean build
A warning does not stop the build. This program declares a variable it never reads, which is the most common warning of a beginner:
int unused = 42;
Console.WriteLine("the build is the subject of this program");
dotnet build
Program.cs(1,5): warning CS0219: The variable 'unused' is assigned but its value is never used
Build succeeded.
Program.cs(1,5): warning CS0219: The variable 'unused' is assigned but its value is never used
1 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.60
The build succeeded, the program runs, and the variable is simply not used. A team that wants every warning fixed turns it into a failure instead.
TreatWarningsAsErrors
The MSBuild property TreatWarningsAsErrors makes the compiler report warnings as errors. It can be set in the project file, or for one build on the command line:
dotnet build --no-incremental -p:TreatWarningsAsErrors=true
Program.cs(1,5): error CS0219: The variable 'unused' is assigned but its value is never used
Build FAILED.
Program.cs(1,5): error CS0219: The variable 'unused' is assigned but its value is never used
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:03.23
The same line, the same code, one word changed: warning
became error, the counts moved, and the build left
status 1. Nothing else about the program changed.
In the project file the property sits in a
PropertyGroup, and a team usually turns it on for
every build:
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
Why a rebuild is needed
The command above carries --no-incremental for a reason. A build compares the timestamps of the source and of the assembly, and if nothing changed it does not run the compiler at all. The second command would then print:
Build succeeded.
0 Warning(s)
0 Error(s)
and report success, which is the wrong answer: the warning is still in the
source, the compiler simply was not asked. That output is what this
container printed for a second dotnet build of the
same file with the property.
--no-incremental compiles everything again, and
dotnet clean before the build does the same with two
commands. When you try a flag on a file that was built before, one of the
two is required.
NoWarn and the exceptions
The other direction exists too: a code that the team has decided to live with is silenced with NoWarn, which takes the codes separated by semicolons.
dotnet build --no-incremental -p:NoWarn=CS0219
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.85
The line is gone and the build is clean, although the variable is still
unused. A NoWarn list grows unless somebody prunes
it, and each entry is a rule the project no longer enforces.
A single line can also be silenced in the source with a pragma, which is the
form to use when the warning is right in general and wrong here:
#pragma warning disable CS0219
int unused = 42;
#pragma warning restore CS0219
Console.WriteLine("the pragma covers one line");
dotnet build --no-incremental
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:04.34
Which warnings to keep
Turning every warning into an error is a decision about a codebase, not a rule of the language. Three warnings are worth an error in almost every project, because each one is a bug in waiting:
- CS8600, CS8602 and the other nullable warnings: a null that reaches a dereference throws.
- CS0168 and CS0219: an unused variable usually means a line was left half-written.
- CS0162: unreachable code, which is either a wrong condition or a deleted feature.
Two are worth keeping as warnings: an obsolete API (CS0618) may be the only one that still works, and a platform warning (CA1416) needs a decision the compiler cannot make. The lesson on the analyzers of the SDK goes further and shows a rule that only the analyzer sees.
Exercises
- Build the first program twice without any flag. Which output does the second build give, and why?
- Set TreatWarningsAsErrors in the project file and build. Then add -p:TreatWarningsAsErrors=false and build again. Which one wins?
- Silence CS0219 with a pragma and check that the warning is gone while the unused variable is still there.
- Add -warnaserror to the build command and read the error. Why is it not a dotnet build option?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does TreatWarningsAsErrors do?
Answer
It reports warnings as errors, so a warning fails the build
The property changes the severity the compiler reports, so a warning leaves status 1 and no assembly is written.
-
Why does a second build with -p:TreatWarningsAsErrors=true print "Build succeeded" and no warning?
Answer
The build was incremental and did not run the compiler again
An up-to-date project skips the compiler, so no diagnostic is produced; --no-incremental or dotnet clean forces the compile.
-
What does this build report?
int unused = 42; Console.WriteLine("done");Answer
warning CS0219: The variable 'unused' is assigned but its value is never used
The variable is assigned and never read, which is CS0219 as a warning; the build still succeeds.
-
What does -p:NoWarn=CS0219 do?
Answer
It suppresses that warning code for the build
NoWarn takes the codes to silence, and the rule is then not reported at all.
-
What does this program print?
int count = 3; count = 4; Console.WriteLine(count);Answer
4
The last assignment wins; CS0219 is about a variable that is never read, and this one is read by WriteLine, so no warning is printed.
-
Which form silences one warning in one place of the source?
Answer
#pragma warning disable CS0219 around the line
The pragma applies from the disable to the restore and is scoped to the file, while NoWarn applies to the whole project.
-
What does
dotnet build -p:TreatWarningsAsErrors=falsedo in a project whose file sets the property to true?Answer
The command line wins, so warnings stay warnings
An MSBuild property given with -p overrides the value in the file for that build.
-
Which warning reports code that can never run?
Answer
CS0162
CS0162 is unreachable code; CS0219 and CS0168 are about unused variables, and CS8600 is a nullable conversion.
-
What does this build report?
Console.WriteLine("start"); return; Console.WriteLine("end");Answer
warning CS0162: Unreachable code detected
The statement after return can never run, so the compiler warns with CS0162 and still builds the program.
-
Why keep CS0618 (an obsolete member) a warning rather than an error?
Answer
The member may still be the only one that works; when to migrate is a decision for the team
An obsolete call compiles and often runs; making it fatal would block a build that must ship before the migration is done.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev