From af1befba7274a23f2ba543ea4235e043e1d47c48 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 2 May 2022 17:33:22 -0400 Subject: [PATCH 01/12] first draft done. First drat is complete. --- .openpublishing.redirection.csharp.json | 4 + .../compiler-messages/cs8892.md | 66 ----- .../ImportedTypes/ImportedTypes.csproj | 9 + .../snippets/ImportedTypes/Types.cs | 9 + .../snippets/WarningWaves/Program.cs | 31 ++ .../snippets/WarningWaves/WarningWaves.csproj | 16 + .../snippets/WarningWaves/WaveFive.cs | 211 +++++++++++++ .../snippets/WarningWaves/WaveSeven.cs | 7 + .../snippets/WarningWaves/WaveSix.cs | 44 +++ .../compiler-messages/warning-waves.md | 278 ++++++++++++++++++ .../compiler-options/errors-warnings.md | 6 +- 11 files changed, 614 insertions(+), 67 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/cs8892.md create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/ImportedTypes.csproj create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/Types.cs create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/Program.cs create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WarningWaves.csproj create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSeven.cs create mode 100644 docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSix.cs create mode 100644 docs/csharp/language-reference/compiler-messages/warning-waves.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 298a406f1d4e6..56820abeb011c 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -151,6 +151,10 @@ "source_path_from_root": "/docs/csharp/lambda-expressions.md", "redirect_url": "/dotnet/csharp/language-reference/operators/lambda-expressions" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8892.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/warning-waves#cs8892" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-options/addmodule-compiler-option.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-options/inputs" diff --git a/docs/csharp/language-reference/compiler-messages/cs8892.md b/docs/csharp/language-reference/compiler-messages/cs8892.md deleted file mode 100644 index 6b2571abd903f..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs8892.md +++ /dev/null @@ -1,66 +0,0 @@ ---- -description: "Learn more about: Compiler warning (level 5) CS8892" -title: "Compiler warning (level 5) CS8892" -ms.date: 08/26/2020 -f1_keywords: - - "CS8892" -helpviewer_keywords: - - "CS8892" -author: Youssef1313 ---- - -# Compiler warning (level 5) CS8892 - -Method 'method' will not be used as an entry point because a synchronous entry point 'method' was found. - -This warning is generated on all async entry point candidates when you have multiple valid entry points, where they contain one or more synchronous entry point and one or more asynchronous entry points. - -Because async main was only supported starting with C# 7.1, this warning isn't generated for projects targeting a previous version. - -> [!NOTE] -> The compiler always uses the synchronous entry point. In case there are multiple synchronous entry points, you get a compiler error. - -## Example - -The following example generates CS8892: - -```csharp -using System; -using System.Threading.Tasks; - -public class Program -{ - // CS8892: Method 'Program.Main()' will not be used as an entry point because a synchronous entry point 'Program.Main(string[])' was found. - public static async Task Main() - { - await Task.Delay(1); - } - - public static void Main(string[] args) - { - Console.WriteLine(2); - } -} -``` - -## How to fix this warning - -Keep only the intended entry point for your program, and rename the others. - -```csharp -using System; -using System.Threading.Tasks; - -public class Program -{ - public static async Task SomeOtherNameAsync() - { - await Task.Delay(1); - } - - public static void Main(string[] args) - { - Console.WriteLine(2); - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/ImportedTypes.csproj b/docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/ImportedTypes.csproj new file mode 100644 index 0000000000000..132c02c59c236 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/ImportedTypes.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/Types.cs b/docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/Types.cs new file mode 100644 index 0000000000000..534a79ce7c41a --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/ImportedTypes/Types.cs @@ -0,0 +1,9 @@ +namespace ImportedTypes; + +// +public struct Struct +{ + private string data = String.Empty; + public Struct() { } +} +// diff --git a/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/Program.cs b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/Program.cs new file mode 100644 index 0000000000000..6e89e1c139292 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/Program.cs @@ -0,0 +1,31 @@ +// See https://aka.ms/new-console-template for more information + +namespace WarningWaves; + +public static class Program +{ + // + public static void Main() + { + RunProgram(); + } + + // CS8892 + public static async Task Main(string[] args) + { + await RunProgramAsync(); + } + // + + private static void RunProgram() + { + WaveFive.Examples(); + } + + private static async Task RunProgramAsync() + { + RunProgram(); + await Task.Yield(); + } +} + diff --git a/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WarningWaves.csproj b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WarningWaves.csproj new file mode 100644 index 0000000000000..4494a6336e3c5 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WarningWaves.csproj @@ -0,0 +1,16 @@ + + + + enable + Exe + net7.0 + enable + 7 + preview + + + + + + + diff --git a/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs new file mode 100644 index 0000000000000..74d2c3ab4b074 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs @@ -0,0 +1,211 @@ +using System; +using ImportedTypes; + +namespace WarningWaves; + +public class WaveFive +{ + public class Src + { + public bool Select(Func selector) => true; + } + + public class Src2 + { + public int Select(Func selector) => 0; + } + + public static void Examples() + { + var w = new WaveFive(); + + w.M(w); + + QueryExpression(); + QueryExpressionNoWarn(); + + } + + private static void QueryExpression() + { + // + bool b = true; + var source = new Src(); + b = true; + source = new Src(); + var a = b && from c in source select c; + Console.WriteLine(a); + + var indexes = new Src2(); + int[] array = { 1, 2, 3, 4, 5, 6, 7 }; + var range = array[0..from c in indexes select c]; + // + } + + private static void QueryExpressionNoWarn() + { + // + bool b = true; + var source = new Src(); + b = true; + source = new Src(); + var a = b && (from c in source select c); + Console.WriteLine(a); + + var indexes = new Src2(); + int[] array = { 1, 2, 3, 4, 5, 6, 7 }; + var range = array[0..(from c in indexes select c)]; + // + } + // + static class StaticClass + { + public static void Thing() { } + } + + void M(object o) + { + // warning: cannot use a static type in 'is' or 'as' + if (o is StaticClass) + { + Console.WriteLine("Can't happen"); + } + else + { + Console.WriteLine("o is not an instance of a static class"); + } + } + // + + // + class Program + { + public static void M(S s) + { + if (s == null) { } // CS8073: The result of the expression is always 'false' + if (s != null) { } // CS8073: The result of the expression is always 'true' + } + } + + struct S + { + public static bool operator ==(S s1, S s2) => s1.Equals(s2); + public static bool operator !=(S s1, S s2) => !s1.Equals(s2); + public override bool Equals(object? other) + { + // Implementation elided + return false; + } + public override int GetHashCode() => 0; + + // Other details elided... + } + // + + // + public static class Utilities + { + // elided + } + + public interface IUtility + { + // CS8897 + public void SetUtility(Utilities u); + + // CS8898 + public Utilities GetUtility(); + } + // + + + // + public struct UninitializedProperty + { + public Struct Property { get; } + + // CS8880 + public UninitializedProperty(int dummy) + { + } + } + // + + // + public struct UninitializedField + { + private Struct field; + + // CS8881 + public UninitializedField(int dummy) + { + } + } + // + + // + // CS8882 + public void Method(out Struct s) + { + + } + // + + // + public struct ClientStruct + { + public Struct Property { get; } + ClientStruct(int dummy) + { + // CS8883 + Struct v2 = Property; + Property = default; + } + } + // + + // + public struct AccessField + { + public Struct Field; + AccessField(int dummy) + { + // CS8884 + Struct v2 = Field; + Field = default; + } + } + // + + // + public struct AccessThis + { + public Struct Field; + AccessThis(int dummy) + { + // CS8885: + AccessThis p2 = this; + this.Field = default; + } + } + // + + + // + public static void Method2(out Struct s1) + { + // CS8886 + var s2 = s1; + s1 = default; + } + // + + // + public static void UseLocalStruct() + { + Struct r1; + var r2 = r1; + } + // + +} diff --git a/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSeven.cs b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSeven.cs new file mode 100644 index 0000000000000..fa8f3498e214b --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSeven.cs @@ -0,0 +1,7 @@ +namespace WarningWaves; + +// +public class lowercasename +{ +} +// diff --git a/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSix.cs b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSix.cs new file mode 100644 index 0000000000000..5c800289b479b --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveSix.cs @@ -0,0 +1,44 @@ +namespace WarningWaves; +public class WaveSix +{ + // + public partial class PartialType + { + public partial void M1(int x); + + public partial T M2(string s) where T : struct; + + public partial void M3(string s); + + + public partial void M4(object o); + public partial void M5(dynamic o); + public partial void M6(string? s); + } + // + + // + public partial class PartialType + { + // Different parameter names: + public partial void M1(int y) { } + + // Different type parameter names: + public partial TResult M2(string s) where TResult : struct => default; + + // Relaxed nullability + public partial void M3(string? s) { } + + + // Mixing object and dynamic + public partial void M4(dynamic o) { } + + // Mixing object and dynamic + public partial void M5(object o) { } + + // Note: This generates CS8611 (nullability mismatch) not CS8826 + public partial void M6(string s) { } + } + // + +} diff --git a/docs/csharp/language-reference/compiler-messages/warning-waves.md b/docs/csharp/language-reference/compiler-messages/warning-waves.md new file mode 100644 index 0000000000000..b3d11a189e1e7 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/warning-waves.md @@ -0,0 +1,278 @@ +--- +title: "C# Compiler warning waves" +description: "C# warning waves are optional warnings that can be reported on code where previously a warning would not have been reported. They represent practices that could be harmful, or potentially elements that may be breaking changes in the future." +ms.date: 04/27/2022 +f1_keywords: + - "CS7023" + - "CS8073" + - "CS8848" + - "CS8880" + - "CS8881" + - "CS8882" + - "CS8883" + - "CS8884" + - "CS8885" + - "CS8886" + - "CS8887" + - "CS8892" + - "CS8897" + - "CS8898" + - "CS8826" + - "CS8981" +helpviewer_keywords: + - "CS7023" + - "CS8073" + - "CS8848" + - "CS8880" + - "CS8881" + - "CS8882" + - "CS8883" + - "CS8884" + - "CS8885" + - "CS8886" + - "CS8887" + - "CS8892" + - "CS8897" + - "CS8898" + - "CS8826" + - "CS8981" +--- +# C# Warning waves + +New warnings and errors may 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 should not see new warnings on existing code without taking action to enable them. Warning waves are enabled using a whole number greater than 4 for the [**WarningLevel**](../compiler-options/errors-warnings.md#warninglevel) element in your project file. When `true` is specified, enabled warning wave warnings generate errors. + +The default warning level is `4`. If you want the compiler to produce all applicable warnings, you can specify `9999`. + +## Warning level 7 + +The compiler shipped with .NET 7 (the C# 11 compiler) contains the following warnings which are reported only under `/warn:7` or higher. + +### CS8981 + +CS8981 - The type name 'name' only contains lower-cased ascii characters. Such names may become reserved for the language. + +Any potential new keywords added for C# will be all lower-case ASCII characters. This warning ensures that none of your types conflict with potential future keywords. The following code produces CS8981: + +:::code language="csharp" source="./snippets/WarningWaves/WaveSeven.cs" id="NoLowercaseTypes"::: + +You can address this warning by renaming the type to include at least non-lower case ASCII character, such as an upper case character, a digit, or an underscore. + +## Warning level 6 + +The compiler shipped with .NET 6 (the C# 10 compiler) contains the following warnings which are reported only under `/warn:6` or higher. + +### CS8826 + +CS8826 - Partial method declarations 'name' and 'name' have signature differences. + +This warning corrects some inconsistencies in reporting differences between partial method signatures. The compiler always reported an error when the partial method signatures created different CLR signatures. Now, the compiler reports CS8826 when the signatures are syntactically different C#. Consider the following partial class: + +:::code language="csharp" source="./snippets/WarningWaves/WaveSix.cs" id="PartialMethodDeclaration"::: + +The following partial class implementation generates several examples of CS8626: + +:::code language="csharp" source="./snippets/WarningWaves/WaveSix.cs" id="PartialMethodDefinition"::: + +Note that if the implementation of a method uses a non-nullable reference type when the other declaration accepts nullable reference types, CS8611 is generated instead of CS8826. + +To fix any instance of these warnings, ensure the two signatures match. + +## Warning level 5 + +Warning level 5 warnings are available beginning with C# 9, which shipped with .NET 5. + +### CS7023 + +CS7023 - A static type is used in an `is` or `as` expression. + +The `is` and `as` expressions always return `false` for a static type because you can't create instances of a static type. The following code produces CS7023: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StaticTypeAsIs"::: + +The compiler reports this warning because the type test can never succeed. To correct this warning, remove the test and remove any code executed only if the test succeeded. In the preceding example, the `else` clause is always executed. The method body could be replaced by that single line: + +```csharp +Console.WriteLine("o is not an instance of a static class"); +``` + +### CS8073 + +CS8073 - The result of the expression is always 'false' (or `true`). + +The `==` and `!=` operators always return `false` (or `true`) when comparing an instance of a `struct` type to `null`. The following code demonstrates this warning. Assume `S` is a `struct` that has defined `operator ==` and `operator !=`: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StructsArentNull"::: + +To fix this error, remove the null check, and and code that would execute if the object is `null`. + +### CS8848 + +CS8848: Operator 'from' cannot be used here due to precedence. Use parentheses to disambiguate. + +The following examples demonstrate this warning. The expression binds incorrectly because of the precedence of the operators. + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="QueryPrecedence"::: + +To fix this error, put parentheses around the query expression: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="QueryPrecedenceNoWarn"::: + +### Definite assignment warnings + +Warning wave 5 includes several warnings the improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) that isn't initialized by that struct. The following struct definition shows an example of a struct with a private field that isn't initialized by the struct: + +:::code language="csharp" source="snippets/ImportedTypes/Types.cs" id="DeclareImportedType"::: + +#### CS8880 + +CS8880: Auto-implemented property 'Property' must be fully assigned before control is returned to the caller. + +The compiler generates CS8880 when you declare an auto implemented property whose type is an imported struct type and don't initialize that struct, as shown in the following code: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UninitializedAutoProp"::: + +To address this warning, explicitly initialize the struct: + +```csharp +public Testing(int dummy) +{ + Property = default; +} +``` + +#### CS8881 + +CS8881: Field 'field' must be fully assigned before control is returned to the caller. + +The compiler generates CS8881 when you declare a field whose type is an imported struct type and don't initialize that struct, as shown in the following code: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UninitializedField"::: + +To address this warning, explicitly initialize the struct: + +```csharp +public Testing(int dummy) +{ + field = default; +} +``` + +#### CS8882 + +CS882: The out parameter 'parameter' must be assigned to before control leaves the current method. + +The compiler generates CS8882 when you declare a method with an `out` parameter whose type is an imported struct type and you don't assign that struct in the method. The following code generates CS8881: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UninitializedField"::: + +To address this warning, explicitly initialize the struct: + +```csharp +private Struct field = default; +``` + +#### CS8883 + +CS8883: Use of possibly unassigned auto-implemented property 'Property'. + +The compiler generates CS8883 when you access an auto-implemented property whose type is an imported struct and you read that property before you definitely assign it. The following code generates CS8883: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UseBeforeAssignment"::: + +To address this warning, explicitly initialize the struct: + +```csharp +public Struct Property { get; } = default; +``` + +#### CS8884 + +CS8884: Use of possibly unassigned field 'Field' + +The compiler generates CS8884 when you access a field whose type is an imported struct and you read that field before you definitely assign it. The following code generates CS8884: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="AccessFieldBeforeAssignment"::: + +To address the warning, explicitly initialize the struct: + +```csharp +public Struct Field; +``` + +#### CS8885 + +CS8885: The 'this' object can't be used before all its fields have been assigned. + +The compiler generates CS8885 when you access `this` before you've assigned all the fields (or auto-implemented properties) in the current instance. The following code generates CS8885: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="AccessThisBeforeAssignment"::: + +To address the warning, explicitly initialize all fields in the struct: + +```csharp +public Struct Field = default; +``` + +#### CS8886 + +CS8886: Use of unassigned output parameter `parameterName` + +The compiler generates CS8886 when you access an out parameter whose type is an imported struct and you read that field out parameter before you definitely assign it. The following code generates CS8886: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UseOutBeforeAssignment"::: + +To address this warning, definitely assign the parameter before accessing it: + +```csharp +s1 = default; +var s2 = s1; +``` + +#### CS8887 + +CS8887: Use of unassigned local variable 'variableName' + +The compiler generates CS8887 when you access a local variable whose type is an imported struct and you read that local variable before you definitely assign it. The following code generates CS8887: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UseLocalStruct"::: + +The address this warning, definitely assign the local variable: + +```csharp +Struct r1 = default; +``` + +### CS8892 + +CS889s: Method 'method' will not be used as an entry point because a synchronous entry point 'method' was found. + +This warning is generated on all async entry point candidates when you have multiple valid entry points, where they contain one or more synchronous entry point and one or more asynchronous entry points. Because async main was only supported starting with C# 7.1, this warning isn't generated for projects targeting a previous version. + +The following example generates CS8892: + +:::code language="csharp" source="./snippets/WarningWaves/Program.cs" id="MultipleEntryPoints"::: + +> [!NOTE] +> The compiler always uses the synchronous entry point. In case there are multiple synchronous entry points, you get a compiler error. + +To fix this warning, remove or rename the asynchronous entry point. + +### CS8897 + +CS8897: static types cannot be used as parameters + +Members of an interface can't declare parameters whose type is a static class. The following code demonstrates both CS8897 and CS8898: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StaticTypeInInterface"::: + +To fix this warning, change the parameter type or remove the method. + +### CS8898 + +CS8898: static types cannot be used as return types + +Members of an interface can't declare a return type that is a static class. The following code demonstrates both CS8897 and CS8898: + +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StaticTypeInInterface"::: + +To fix this warning, change the return type or remove the method. diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index 79b945cfd10c5..5247fde65b531 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -44,7 +44,11 @@ The element value is the warning level you want displayed for the compilation: L |2|Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members.| |3|Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to `true` or `false`.| |4 (the default)|Displays all level 3 warnings plus informational warnings.| -|Greater than 4|Displays warnings with the specified value and all less-severe warnings. For more information, see [additional warnings](https://github.com/dotnet/roslyn/blob/main/docs/compilers/CSharp/Warnversion%20Warning%20Waves.md).| +|5 |Displays all informational warnings, and optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#warning-level-5).| +|6 |Displays all informational warnings, and optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#warning-level-6).| +|7 |Displays all informational warnings, and optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#warning-level-7).| + +For more information on optiona warnings, see [Warning waves](../compiler-messages/warning-waves.md).| > [!NOTE] > To make sure you always have all warnings if the compiler is updated with new warning levels, put an arbitrary large value (for example, `9999`). From dcca45e76869b4d3c6f77e82570b04c686ba8965 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 4 May 2022 17:01:46 -0400 Subject: [PATCH 02/12] Combine all definite assignment warnings I think this reads better. It's much less redundant, and these different warnings are related. --- .../snippets/WarningWaves/WaveFive.cs | 117 ++++++++-------- .../compiler-messages/warning-waves.md | 127 ++---------------- 2 files changed, 66 insertions(+), 178 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs index 74d2c3ab4b074..5b80ceb43326d 100644 --- a/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs +++ b/docs/csharp/language-reference/compiler-messages/snippets/WarningWaves/WaveFive.cs @@ -119,93 +119,82 @@ public interface IUtility // - // - public struct UninitializedProperty + // + public struct DefiniteAssignmentWarnings { + // CS8880 public Struct Property { get; } + // CS8881 + private Struct field; - // CS8880 - public UninitializedProperty(int dummy) + // CS8882 + public void Method(out Struct s) { + } - } - // - // - public struct UninitializedField - { - private Struct field; + public DefiniteAssignmentWarnings(int dummy) + { + // CS8883 + Struct v2 = Property; + // CS8884 + Struct v3 = field; + // CS8885: + DefiniteAssignmentWarnings p2 = this; + } - // CS8881 - public UninitializedField(int dummy) + public static void Method2(out Struct s1) + { + // CS8886 + var s2 = s1; + s1 = default; + } + + public static void UseLocalStruct() { + Struct r1; + var r2 = r1; } } - // + // + - // - // CS8882 - public void Method(out Struct s) + // + public struct DefiniteAssignmentNoWarnings { + // CS8880 + public Struct Property { get; } = default; + // CS8881 + private Struct field = default; - } - // + // CS8882 + public void Method(out Struct s) + { + s = default; + } - // - public struct ClientStruct - { - public Struct Property { get; } - ClientStruct(int dummy) + public DefiniteAssignmentNoWarnings(int dummy) { // CS8883 Struct v2 = Property; - Property = default; + // CS8884 + Struct v3 = field; + // CS8885: + DefiniteAssignmentNoWarnings p2 = this; } - } - // - // - public struct AccessField - { - public Struct Field; - AccessField(int dummy) + public static void Method2(out Struct s1) { - // CS8884 - Struct v2 = Field; - Field = default; + // CS8886 + s1 = default; + var s2 = s1; } - } - // - // - public struct AccessThis - { - public Struct Field; - AccessThis(int dummy) + public static void UseLocalStruct() { - // CS8885: - AccessThis p2 = this; - this.Field = default; + Struct r1 = default; + var r2 = r1; } } - // - - - // - public static void Method2(out Struct s1) - { - // CS8886 - var s2 = s1; - s1 = default; - } - // - - // - public static void UseLocalStruct() - { - Struct r1; - var r2 = r1; - } - // - + // } diff --git a/docs/csharp/language-reference/compiler-messages/warning-waves.md b/docs/csharp/language-reference/compiler-messages/warning-waves.md index b3d11a189e1e7..34d88e80d7387 100644 --- a/docs/csharp/language-reference/compiler-messages/warning-waves.md +++ b/docs/csharp/language-reference/compiler-messages/warning-waves.md @@ -119,128 +119,27 @@ To fix this error, put parentheses around the query expression: ### Definite assignment warnings -Warning wave 5 includes several warnings the improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) that isn't initialized by that struct. The following struct definition shows an example of a struct with a private field that isn't initialized by the struct: +Warning wave 5 includes several warnings the improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) of a reference type, as shown in the following example: :::code language="csharp" source="snippets/ImportedTypes/Types.cs" id="DeclareImportedType"::: -#### CS8880 +The following examples show the warnings generated from the improved definite assignment analysis: -CS8880: Auto-implemented property 'Property' must be fully assigned before control is returned to the caller. +- CS8880: Auto-implemented property 'Property' must be fully assigned before control is returned to the caller. +- CS8881: Field 'field' must be fully assigned before control is returned to the caller. +- CS882: The out parameter 'parameter' must be assigned to before control leaves the current method. +- CS8883: Use of possibly unassigned auto-implemented property 'Property'. +- CS8884: Use of possibly unassigned field 'Field' +- CS8885: The 'this' object can't be used before all its fields have been assigned. +- CS8886: Use of unassigned output parameter `parameterName` +- CS8887: Use of unassigned local variable 'variableName' -The compiler generates CS8880 when you declare an auto implemented property whose type is an imported struct type and don't initialize that struct, as shown in the following code: +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="DefiniteAssignmentWarnings"::: -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UninitializedAutoProp"::: +You can fix any of these warnings by initializing or assigning the imported struct to its default value: -To address this warning, explicitly initialize the struct: +:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="DefiniteAssignment"::: -```csharp -public Testing(int dummy) -{ - Property = default; -} -``` - -#### CS8881 - -CS8881: Field 'field' must be fully assigned before control is returned to the caller. - -The compiler generates CS8881 when you declare a field whose type is an imported struct type and don't initialize that struct, as shown in the following code: - -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UninitializedField"::: - -To address this warning, explicitly initialize the struct: - -```csharp -public Testing(int dummy) -{ - field = default; -} -``` - -#### CS8882 - -CS882: The out parameter 'parameter' must be assigned to before control leaves the current method. - -The compiler generates CS8882 when you declare a method with an `out` parameter whose type is an imported struct type and you don't assign that struct in the method. The following code generates CS8881: - -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UninitializedField"::: - -To address this warning, explicitly initialize the struct: - -```csharp -private Struct field = default; -``` - -#### CS8883 - -CS8883: Use of possibly unassigned auto-implemented property 'Property'. - -The compiler generates CS8883 when you access an auto-implemented property whose type is an imported struct and you read that property before you definitely assign it. The following code generates CS8883: - -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UseBeforeAssignment"::: - -To address this warning, explicitly initialize the struct: - -```csharp -public Struct Property { get; } = default; -``` - -#### CS8884 - -CS8884: Use of possibly unassigned field 'Field' - -The compiler generates CS8884 when you access a field whose type is an imported struct and you read that field before you definitely assign it. The following code generates CS8884: - -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="AccessFieldBeforeAssignment"::: - -To address the warning, explicitly initialize the struct: - -```csharp -public Struct Field; -``` - -#### CS8885 - -CS8885: The 'this' object can't be used before all its fields have been assigned. - -The compiler generates CS8885 when you access `this` before you've assigned all the fields (or auto-implemented properties) in the current instance. The following code generates CS8885: - -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="AccessThisBeforeAssignment"::: - -To address the warning, explicitly initialize all fields in the struct: - -```csharp -public Struct Field = default; -``` - -#### CS8886 - -CS8886: Use of unassigned output parameter `parameterName` - -The compiler generates CS8886 when you access an out parameter whose type is an imported struct and you read that field out parameter before you definitely assign it. The following code generates CS8886: - -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UseOutBeforeAssignment"::: - -To address this warning, definitely assign the parameter before accessing it: - -```csharp -s1 = default; -var s2 = s1; -``` - -#### CS8887 - -CS8887: Use of unassigned local variable 'variableName' - -The compiler generates CS8887 when you access a local variable whose type is an imported struct and you read that local variable before you definitely assign it. The following code generates CS8887: - -:::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="UseLocalStruct"::: - -The address this warning, definitely assign the local variable: - -```csharp -Struct r1 = default; -``` ### CS8892 From c66953d3b35809b053ac1afbd92906a75df61b88 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 4 May 2022 17:09:42 -0400 Subject: [PATCH 03/12] proofread and link from what's new --- .../compiler-messages/warning-waves.md | 24 +++++++++---------- docs/csharp/whats-new/csharp-10.md | 1 + docs/csharp/whats-new/csharp-11.md | 1 + docs/csharp/whats-new/csharp-9.md | 1 + 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/warning-waves.md b/docs/csharp/language-reference/compiler-messages/warning-waves.md index 34d88e80d7387..c7d62f662ba33 100644 --- a/docs/csharp/language-reference/compiler-messages/warning-waves.md +++ b/docs/csharp/language-reference/compiler-messages/warning-waves.md @@ -1,6 +1,6 @@ --- title: "C# Compiler warning waves" -description: "C# warning waves are optional warnings that can be reported on code where previously a warning would not have been reported. They represent practices that could be harmful, or potentially elements that may be breaking changes in the future." +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 may be breaking changes in the future." ms.date: 04/27/2022 f1_keywords: - "CS7023" @@ -39,13 +39,13 @@ helpviewer_keywords: --- # C# Warning waves -New warnings and errors may 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 should not see new warnings on existing code without taking action to enable them. Warning waves are enabled using a whole number greater than 4 for the [**WarningLevel**](../compiler-options/errors-warnings.md#warninglevel) element in your project file. When `true` is specified, enabled warning wave warnings generate errors. +New warnings and errors may 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 a whole number greater than 4 for the [**WarningLevel**](../compiler-options/errors-warnings.md#warninglevel) element in your project file. When `true` is specified, enabled warning wave warnings generate errors. The default warning level is `4`. If you want the compiler to produce all applicable warnings, you can specify `9999`. ## Warning level 7 -The compiler shipped with .NET 7 (the C# 11 compiler) contains the following warnings which are reported only under `/warn:7` or higher. +The compiler shipped with .NET 7 (the C# 11 compiler) contains the following warnings that are reported only under `/warn:7` or higher. ### CS8981 @@ -59,7 +59,7 @@ You can address this warning by renaming the type to include at least non-lower ## Warning level 6 -The compiler shipped with .NET 6 (the C# 10 compiler) contains the following warnings which are reported only under `/warn:6` or higher. +The compiler shipped with .NET 6 (the C# 10 compiler) contains the following warnings that are reported only under `/warn:6` or higher. ### CS8826 @@ -73,7 +73,8 @@ The following partial class implementation generates several examples of CS8626: :::code language="csharp" source="./snippets/WarningWaves/WaveSix.cs" id="PartialMethodDefinition"::: -Note that if the implementation of a method uses a non-nullable reference type when the other declaration accepts nullable reference types, CS8611 is generated instead of CS8826. +> [!NOTE] +> If the implementation of a method uses a non-nullable reference type when the other declaration accepts nullable reference types, CS8611 is generated instead of CS8826. To fix any instance of these warnings, ensure the two signatures match. @@ -103,11 +104,11 @@ The `==` and `!=` operators always return `false` (or `true`) when comparing an :::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StructsArentNull"::: -To fix this error, remove the null check, and and code that would execute if the object is `null`. +To fix this error, remove the null check, and code that would execute if the object is `null`. ### CS8848 -CS8848: Operator 'from' cannot be used here due to precedence. Use parentheses to disambiguate. +CS8848: Operator 'from' can't be used here due to precedence. Use parentheses to disambiguate. The following examples demonstrate this warning. The expression binds incorrectly because of the precedence of the operators. @@ -119,7 +120,7 @@ To fix this error, put parentheses around the query expression: ### Definite assignment warnings -Warning wave 5 includes several warnings the improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) of a reference type, as shown in the following example: +Warning wave 5 includes several warnings that improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) of a reference type, as shown in the following example: :::code language="csharp" source="snippets/ImportedTypes/Types.cs" id="DeclareImportedType"::: @@ -140,12 +141,11 @@ You can fix any of these warnings by initializing or assigning the imported stru :::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="DefiniteAssignment"::: - ### CS8892 CS889s: Method 'method' will not be used as an entry point because a synchronous entry point 'method' was found. -This warning is generated on all async entry point candidates when you have multiple valid entry points, where they contain one or more synchronous entry point and one or more asynchronous entry points. Because async main was only supported starting with C# 7.1, this warning isn't generated for projects targeting a previous version. +This warning is generated on all async entry point candidates when you have multiple valid entry points, including one or more synchronous entry point. Because async main was only supported starting with C# 7.1, this warning isn't generated for projects targeting a previous version. The following example generates CS8892: @@ -158,7 +158,7 @@ To fix this warning, remove or rename the asynchronous entry point. ### CS8897 -CS8897: static types cannot be used as parameters +CS8897: static types can't be used as parameters Members of an interface can't declare parameters whose type is a static class. The following code demonstrates both CS8897 and CS8898: @@ -168,7 +168,7 @@ To fix this warning, change the parameter type or remove the method. ### CS8898 -CS8898: static types cannot be used as return types +CS8898: static types can't be used as return types Members of an interface can't declare a return type that is a static class. The following code demonstrates both CS8897 and CS8898: diff --git a/docs/csharp/whats-new/csharp-10.md b/docs/csharp/whats-new/csharp-10.md index a38832863b5ae..975c0871ffe51 100644 --- a/docs/csharp/whats-new/csharp-10.md +++ b/docs/csharp/whats-new/csharp-10.md @@ -21,6 +21,7 @@ C# 10 adds the following features and enhancements to the C# language: - [Allow `AsyncMethodBuilder` attribute on methods](#allow-asyncmethodbuilder-attribute-on-methods) - [CallerArgumentExpression attribute](#callerargumentexpression-attribute-diagnostics) - [Enhanced `#line` pragma](#enhanced-line-pragma) +- [Warning wave 6](../language-reference/compiler-messages/warning-waves.md#warning-level-6) C# 10 is supported on **.NET 6**. For more information, see [C# language versioning](../language-reference/configure-language-version.md). diff --git a/docs/csharp/whats-new/csharp-11.md b/docs/csharp/whats-new/csharp-11.md index bdbb7aa7e3a6e..2b5139a0aa2ce 100644 --- a/docs/csharp/whats-new/csharp-11.md +++ b/docs/csharp/whats-new/csharp-11.md @@ -18,6 +18,7 @@ The following features are available in the 6.0.200 version of the .NET SDK. The - [Newlines in string interpolation expressions](#newlines-in-string-interpolations). - [Improved method group conversion to delegate](#improved-method-group-conversion-to-delegate) - [Raw string literals](#raw-string-literals). +- [Warning wave 7](../language-reference/compiler-messages/warning-waves.md#warning-level-7) You can download the latest .NET 6 SDK from the [.NET downloads page](https://dotnet.microsoft.com/download). You can also download [Visual Studio 2022](https://visualstudio.microsoft.com/vs/), which includes the .NET 6 SDK. You can also try all these features with the preview release of the .NET 7 SDK, which can be downloaded from the [all .NET downloads](https://dotnet.microsoft.com/download/dotnet) page. diff --git a/docs/csharp/whats-new/csharp-9.md b/docs/csharp/whats-new/csharp-9.md index c1bdc17d3ffe3..a188471c88b2e 100644 --- a/docs/csharp/whats-new/csharp-9.md +++ b/docs/csharp/whats-new/csharp-9.md @@ -26,6 +26,7 @@ C# 9.0 adds the following features and enhancements to the C# language: - [Support for code generators](#support-for-code-generators) - [Module initializers](~/_csharplang/proposals/csharp-9.0/module-initializers.md) - [New features for partial methods](~/_csharplang/proposals/csharp-9.0/extending-partial-methods.md) +- [Warning wave 5](../language-reference/compiler-messages/warning-waves.md#warning-level-5) C# 9.0 is supported on **.NET 5**. For more information, see [C# language versioning](../language-reference/configure-language-version.md). From c6f5a31fc3e782c90dbea92ebc9a72c2e21169cf Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 May 2022 17:32:16 -0400 Subject: [PATCH 04/12] Update for warning level / analysis level While making those edits, fix other open issues on this page. --- .../compiler-options/errors-warnings.md | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index 5247fde65b531..9d1186a96e69b 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -19,6 +19,7 @@ helpviewer_keywords: The following options control how the compiler reports errors and warnings. The new MSBuild syntax is shown in **Bold**. The older *csc.exe* syntax is shown in `code style`. - **WarningLevel** / `-warn`: Set warning level. +- **AnalysisLevel**: Set optional warning level. - **TreatWarningsAsErrors** / `-warnaserror`: Treat all warnings as errors - **WarningsAsErrors** / `-warnaserror`: Treat one or more warnings as errors - **WarningsNotAsErrors** / `-warnnotaserror`: Treat one or more warnings not as errors @@ -37,21 +38,31 @@ The **WarningLevel** option specifies the warning level for the compiler to disp The element value is the warning level you want displayed for the compilation: Lower numbers show only high severity warnings. Higher numbers show more warnings. The value must be zero or a positive integer: -|Warning level|Meaning| -|-------------------|-------------| -|0|Turns off emission of all warning messages.| -|1|Displays severe warning messages.| -|2|Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members.| -|3|Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to `true` or `false`.| -|4 (the default)|Displays all level 3 warnings plus informational warnings.| -|5 |Displays all informational warnings, and optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#warning-level-5).| -|6 |Displays all informational warnings, and optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#warning-level-6).| -|7 |Displays all informational warnings, and optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#warning-level-7).| +| Warning level | Meaning | +|---------------|---------| +| 0 | Turns off emission of all warning messages. | +| 1 | Displays severe warning messages. | +| 2 | Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members. | +| 3 | Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to `true` or `false`. | +| 4 (the default)|Displays all level 3 warnings plus informational warnings. | -For more information on optiona warnings, see [Warning waves](../compiler-messages/warning-waves.md).| +> [!WARNING] +> 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. -> [!NOTE] -> To make sure you always have all warnings if the compiler is updated with new warning levels, put an arbitrary large value (for example, `9999`). +To get information about an error or warning, you can look up the error code in the Help Index. 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**](#disabledwarnings) to disable certain warnings. + +## Analysis level + +| Analysis level | Meaning | +|----------------|---------| +| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#warning-level-5). | +| 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#warning-level-6). | +| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#warning-level-7). | +| latest | Displays all informational warnings up to and including the current release. | +| preview | Displays all informational warnings up to and including the latest preview release. | +| none | Turns off all informational warnings. | + +For more information on optional warnings, see [Warning waves](../compiler-messages/warning-waves.md).| To get information about an error or warning, you can look up the error code in the Help Index. 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**](#disabledwarnings) to disable certain warnings. @@ -65,20 +76,23 @@ 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 [**DisabledWarnings**](#disabledwarnings) to disable certain warnings. +> [!IMPORTANT] +> There are two subtle differences between using the `` 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. + ## WarningsAsErrors and WarningsNotAsErrors -The **WarningsAsErrors** and **WarningsNotAsErrors** options override the **TreatWarningsAsErrors** option for a list of warnings. +The **WarningsAsErrors** and **WarningsNotAsErrors** options override the **TreatWarningsAsErrors** option for a list of warnings. This option can be used with all *CS* warnings. The "CS" prefix is optional. You can use either the number, or "CS" followed by the error or warning level. For other elements that affect warnings, see the [Common MSBuild properties](/visualstudio/msbuild/common-msbuild-project-properties). Enable warnings 0219 and 0168 as errors: ```xml -0219,0168 +0219,CS0168 ``` Disable the same warnings as errors: ```xml -0219,0168 +0219,CS0168 ``` 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. @@ -119,6 +133,14 @@ Specify a file to log all compiler and analyzer diagnostics. 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. +You can specify the SARIF format using the `version` argument to the `ErrorLog` element: + +```XML +logVersion21.json,version=2.1 +``` + +The separator can be either a comma (`,`) or a semi-colon (`;`). Valid values are for version are: "1", "2", and "2.1". The default is "1". "2" and "2.1" both mean SARIF version 2.1.0. + ## ReportAnalyzer Report additional analyzer information, such as execution time. @@ -128,3 +150,6 @@ Report additional analyzer information, such as execution time. ``` 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. + +> [!IMPORTANT] +> The extra log information generated by this flag is only generated when the `-verbosity:detailed` command line option is used. See the [swtiches](/visualstudio/msbuild/msbuild-command-line-reference#switches) article in the MSBuild documentation for more information. From 3d90574929de844296201a4952aaa67f692f5ff0 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 May 2022 17:52:29 -0400 Subject: [PATCH 05/12] Improve SEO Work to move portions of the messages and error code into H2s. --- .../compiler-messages/warning-waves.md | 58 ++++++++----------- .../compiler-options/errors-warnings.md | 4 +- docs/csharp/whats-new/csharp-11.md | 2 +- 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/warning-waves.md b/docs/csharp/language-reference/compiler-messages/warning-waves.md index c7d62f662ba33..48a971d9f3400 100644 --- a/docs/csharp/language-reference/compiler-messages/warning-waves.md +++ b/docs/csharp/language-reference/compiler-messages/warning-waves.md @@ -1,7 +1,7 @@ --- title: "C# Compiler warning waves" 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 may be breaking changes in the future." -ms.date: 04/27/2022 +ms.date: 05/11/2022 f1_keywords: - "CS7023" - "CS8073" @@ -39,17 +39,11 @@ helpviewer_keywords: --- # C# Warning waves -New warnings and errors may 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 a whole number greater than 4 for the [**WarningLevel**](../compiler-options/errors-warnings.md#warninglevel) element in your project file. When `true` is specified, enabled warning wave warnings generate errors. +New warnings and errors may 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#analysislevel) element in your project file. When `true` 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. -The default warning level is `4`. If you want the compiler to produce all applicable warnings, you can specify `9999`. +## CS8981 - The type name only contains lower-cased ascii character -## Warning level 7 - -The compiler shipped with .NET 7 (the C# 11 compiler) contains the following warnings that are reported only under `/warn:7` or higher. - -### CS8981 - -CS8981 - The type name 'name' only contains lower-cased ascii characters. Such names may become reserved for the language. +*Warning level 7* Any potential new keywords added for C# will be all lower-case ASCII characters. This warning ensures that none of your types conflict with potential future keywords. The following code produces CS8981: @@ -57,13 +51,9 @@ Any potential new keywords added for C# will be all lower-case ASCII characters. You can address this warning by renaming the type to include at least non-lower case ASCII character, such as an upper case character, a digit, or an underscore. -## Warning level 6 - -The compiler shipped with .NET 6 (the C# 10 compiler) contains the following warnings that are reported only under `/warn:6` or higher. +## CS8826 - Partial method declarations have signature differences. -### CS8826 - -CS8826 - Partial method declarations 'name' and 'name' have signature differences. +*Warning level 6* This warning corrects some inconsistencies in reporting differences between partial method signatures. The compiler always reported an error when the partial method signatures created different CLR signatures. Now, the compiler reports CS8826 when the signatures are syntactically different C#. Consider the following partial class: @@ -78,13 +68,9 @@ The following partial class implementation generates several examples of CS8626: To fix any instance of these warnings, ensure the two signatures match. -## Warning level 5 - -Warning level 5 warnings are available beginning with C# 9, which shipped with .NET 5. +## CS7023 - A static type is used in an `is` or `as` expression. -### CS7023 - -CS7023 - A static type is used in an `is` or `as` expression. +*Warning level 5* The `is` and `as` expressions always return `false` for a static type because you can't create instances of a static type. The following code produces CS7023: @@ -96,9 +82,9 @@ The compiler reports this warning because the type test can never succeed. To co Console.WriteLine("o is not an instance of a static class"); ``` -### CS8073 +## CS8073 - The result of the expression is always 'false' (or `true`). -CS8073 - The result of the expression is always 'false' (or `true`). +*Warning level 5* The `==` and `!=` operators always return `false` (or `true`) when comparing an instance of a `struct` type to `null`. The following code demonstrates this warning. Assume `S` is a `struct` that has defined `operator ==` and `operator !=`: @@ -106,9 +92,9 @@ The `==` and `!=` operators always return `false` (or `true`) when comparing an To fix this error, remove the null check, and code that would execute if the object is `null`. -### CS8848 +## CS8848 - Operator 'from' can't be used here due to precedence. Use parentheses to disambiguate. -CS8848: Operator 'from' can't be used here due to precedence. Use parentheses to disambiguate. +*Warning level 5* The following examples demonstrate this warning. The expression binds incorrectly because of the precedence of the operators. @@ -118,9 +104,11 @@ To fix this error, put parentheses around the query expression: :::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="QueryPrecedenceNoWarn"::: -### Definite assignment warnings +## Members must be fully assigned, use of unassigned variable (CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, CS8887) + +*Warning level 5* -Warning wave 5 includes several warnings that improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) of a reference type, as shown in the following example: +Several warnings improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) of a reference type, as shown in the following example: :::code language="csharp" source="snippets/ImportedTypes/Types.cs" id="DeclareImportedType"::: @@ -128,7 +116,7 @@ The following examples show the warnings generated from the improved definite as - CS8880: Auto-implemented property 'Property' must be fully assigned before control is returned to the caller. - CS8881: Field 'field' must be fully assigned before control is returned to the caller. -- CS882: The out parameter 'parameter' must be assigned to before control leaves the current method. +- CS8882: The out parameter 'parameter' must be assigned to before control leaves the current method. - CS8883: Use of possibly unassigned auto-implemented property 'Property'. - CS8884: Use of possibly unassigned field 'Field' - CS8885: The 'this' object can't be used before all its fields have been assigned. @@ -141,9 +129,9 @@ You can fix any of these warnings by initializing or assigning the imported stru :::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="DefiniteAssignment"::: -### CS8892 +## CS8892 - Method will not be used as an entry point because a synchronous entry point 'method' was found. -CS889s: Method 'method' will not be used as an entry point because a synchronous entry point 'method' was found. +*Warning level 5* This warning is generated on all async entry point candidates when you have multiple valid entry points, including one or more synchronous entry point. Because async main was only supported starting with C# 7.1, this warning isn't generated for projects targeting a previous version. @@ -156,9 +144,9 @@ The following example generates CS8892: To fix this warning, remove or rename the asynchronous entry point. -### CS8897 +## CS8897 - static types can't be used as parameters -CS8897: static types can't be used as parameters +*Warning level 5* Members of an interface can't declare parameters whose type is a static class. The following code demonstrates both CS8897 and CS8898: @@ -166,9 +154,9 @@ Members of an interface can't declare parameters whose type is a static class. T To fix this warning, change the parameter type or remove the method. -### CS8898 +## CS8898 - static types can't be used as return types -CS8898: static types can't be used as return types +*Warning level 5* Members of an interface can't declare a return type that is a static class. The following code demonstrates both CS8897 and CS8898: diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index 9d1186a96e69b..22450f647e912 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -1,7 +1,7 @@ --- description: "C# Compiler Options for errors and warnings. These options suppress or enable warnings, and control warnings as errors." title: "C# Compiler Options - errors and warnings" -ms.date: 03/12/2021 +ms.date: 05/11/2022 f1_keywords: - "cs.build.options" helpviewer_keywords: @@ -107,7 +107,7 @@ The **DisabledWarnings** option lets you suppress the compiler from displaying o `number1`, `number2` Warning number(s) that you want the compiler to suppress. You specify the numeric part of the warning identifier. For example, if you want to suppress *CS0028*, you could specify `28`. The compiler silently ignores warning numbers passed to **DisabledWarnings** 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. - The following warnings cannot be suppressed by the **DisabledWarnings** option: + The following warnings can't be suppressed by the **DisabledWarnings** option: - Compiler Warning (level 1) CS2002 - Compiler Warning (level 1) CS2023 diff --git a/docs/csharp/whats-new/csharp-11.md b/docs/csharp/whats-new/csharp-11.md index 2b5139a0aa2ce..5d8b4cbe07fcb 100644 --- a/docs/csharp/whats-new/csharp-11.md +++ b/docs/csharp/whats-new/csharp-11.md @@ -1,7 +1,7 @@ --- title: What's new in C# 11 - C# Guide description: Get an overview of the new features coming in C# 11. -ms.date: 04/15/2022 +ms.date: 05/11/2022 --- # What's new in C# 11 From 810214a737919b9892660e1a44558121ed32175c Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 May 2022 17:56:33 -0400 Subject: [PATCH 06/12] use consistent language for "warning wave" --- .../compiler-messages/warning-waves.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/warning-waves.md b/docs/csharp/language-reference/compiler-messages/warning-waves.md index 48a971d9f3400..0c0076e3122d8 100644 --- a/docs/csharp/language-reference/compiler-messages/warning-waves.md +++ b/docs/csharp/language-reference/compiler-messages/warning-waves.md @@ -43,7 +43,7 @@ New warnings and errors may be introduced in each release of the C# compiler. Wh ## CS8981 - The type name only contains lower-cased ascii character -*Warning level 7* +*Warning wave 7* Any potential new keywords added for C# will be all lower-case ASCII characters. This warning ensures that none of your types conflict with potential future keywords. The following code produces CS8981: @@ -53,7 +53,7 @@ You can address this warning by renaming the type to include at least non-lower ## CS8826 - Partial method declarations have signature differences. -*Warning level 6* +*Warning wave 6* This warning corrects some inconsistencies in reporting differences between partial method signatures. The compiler always reported an error when the partial method signatures created different CLR signatures. Now, the compiler reports CS8826 when the signatures are syntactically different C#. Consider the following partial class: @@ -70,7 +70,7 @@ To fix any instance of these warnings, ensure the two signatures match. ## CS7023 - A static type is used in an `is` or `as` expression. -*Warning level 5* +*Warning wave 5* The `is` and `as` expressions always return `false` for a static type because you can't create instances of a static type. The following code produces CS7023: @@ -84,7 +84,7 @@ Console.WriteLine("o is not an instance of a static class"); ## CS8073 - The result of the expression is always 'false' (or `true`). -*Warning level 5* +*Warning wave 5* The `==` and `!=` operators always return `false` (or `true`) when comparing an instance of a `struct` type to `null`. The following code demonstrates this warning. Assume `S` is a `struct` that has defined `operator ==` and `operator !=`: @@ -94,7 +94,7 @@ To fix this error, remove the null check, and code that would execute if the obj ## CS8848 - Operator 'from' can't be used here due to precedence. Use parentheses to disambiguate. -*Warning level 5* +*Warning wave 5* The following examples demonstrate this warning. The expression binds incorrectly because of the precedence of the operators. @@ -106,7 +106,7 @@ To fix this error, put parentheses around the query expression: ## Members must be fully assigned, use of unassigned variable (CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, CS8887) -*Warning level 5* +*Warning wave 5* Several warnings improve the definite assignment analysis for `struct` types declared in imported assemblies. All these new warnings are generated when a struct in an imported assembly includes an inaccessible field (usually a `private` field) of a reference type, as shown in the following example: @@ -131,7 +131,7 @@ You can fix any of these warnings by initializing or assigning the imported stru ## CS8892 - Method will not be used as an entry point because a synchronous entry point 'method' was found. -*Warning level 5* +*Warning wave 5* This warning is generated on all async entry point candidates when you have multiple valid entry points, including one or more synchronous entry point. Because async main was only supported starting with C# 7.1, this warning isn't generated for projects targeting a previous version. @@ -146,7 +146,7 @@ To fix this warning, remove or rename the asynchronous entry point. ## CS8897 - static types can't be used as parameters -*Warning level 5* +*Warning wave 5* Members of an interface can't declare parameters whose type is a static class. The following code demonstrates both CS8897 and CS8898: @@ -156,7 +156,7 @@ To fix this warning, change the parameter type or remove the method. ## CS8898 - static types can't be used as return types -*Warning level 5* +*Warning wave 5* Members of an interface can't declare a return type that is a static class. The following code demonstrates both CS8897 and CS8898: From ce2081e46ca1a8aff2aae1bc9aa3c1537e057a4f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 May 2022 18:14:10 -0400 Subject: [PATCH 07/12] warnings, part 1 --- .../language-reference/compiler-messages/warning-waves.md | 2 +- .../language-reference/compiler-options/errors-warnings.md | 6 +++--- docs/csharp/whats-new/csharp-10.md | 2 +- docs/csharp/whats-new/csharp-11.md | 2 +- docs/csharp/whats-new/csharp-9.md | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/warning-waves.md b/docs/csharp/language-reference/compiler-messages/warning-waves.md index 0c0076e3122d8..c6e391fa794ca 100644 --- a/docs/csharp/language-reference/compiler-messages/warning-waves.md +++ b/docs/csharp/language-reference/compiler-messages/warning-waves.md @@ -39,7 +39,7 @@ helpviewer_keywords: --- # C# Warning waves -New warnings and errors may 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#analysislevel) element in your project file. When `true` 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. +New warnings and errors may 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 `true` 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. ## CS8981 - The type name only contains lower-cased ascii character diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index 22450f647e912..85db475365ad7 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -55,9 +55,9 @@ To get information about an error or warning, you can look up the error code in | Analysis level | Meaning | |----------------|---------| -| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#warning-level-5). | -| 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#warning-level-6). | -| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#warning-level-7). | +| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#cs7023). | +| 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#cs8826). | +| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#cs8981). | | latest | Displays all informational warnings up to and including the current release. | | preview | Displays all informational warnings up to and including the latest preview release. | | none | Turns off all informational warnings. | diff --git a/docs/csharp/whats-new/csharp-10.md b/docs/csharp/whats-new/csharp-10.md index 975c0871ffe51..89817bd52a408 100644 --- a/docs/csharp/whats-new/csharp-10.md +++ b/docs/csharp/whats-new/csharp-10.md @@ -21,7 +21,7 @@ C# 10 adds the following features and enhancements to the C# language: - [Allow `AsyncMethodBuilder` attribute on methods](#allow-asyncmethodbuilder-attribute-on-methods) - [CallerArgumentExpression attribute](#callerargumentexpression-attribute-diagnostics) - [Enhanced `#line` pragma](#enhanced-line-pragma) -- [Warning wave 6](../language-reference/compiler-messages/warning-waves.md#warning-level-6) +- [Warning wave 6](../language-reference/compiler-messages/warning-waves.md#cs8826) C# 10 is supported on **.NET 6**. For more information, see [C# language versioning](../language-reference/configure-language-version.md). diff --git a/docs/csharp/whats-new/csharp-11.md b/docs/csharp/whats-new/csharp-11.md index 5d8b4cbe07fcb..6d5cec6fd3e74 100644 --- a/docs/csharp/whats-new/csharp-11.md +++ b/docs/csharp/whats-new/csharp-11.md @@ -18,7 +18,7 @@ The following features are available in the 6.0.200 version of the .NET SDK. The - [Newlines in string interpolation expressions](#newlines-in-string-interpolations). - [Improved method group conversion to delegate](#improved-method-group-conversion-to-delegate) - [Raw string literals](#raw-string-literals). -- [Warning wave 7](../language-reference/compiler-messages/warning-waves.md#warning-level-7) +- [Warning wave 7](../language-reference/compiler-messages/warning-waves.md#cs7023) You can download the latest .NET 6 SDK from the [.NET downloads page](https://dotnet.microsoft.com/download). You can also download [Visual Studio 2022](https://visualstudio.microsoft.com/vs/), which includes the .NET 6 SDK. You can also try all these features with the preview release of the .NET 7 SDK, which can be downloaded from the [all .NET downloads](https://dotnet.microsoft.com/download/dotnet) page. diff --git a/docs/csharp/whats-new/csharp-9.md b/docs/csharp/whats-new/csharp-9.md index a188471c88b2e..ba08cc7a4693b 100644 --- a/docs/csharp/whats-new/csharp-9.md +++ b/docs/csharp/whats-new/csharp-9.md @@ -26,7 +26,7 @@ C# 9.0 adds the following features and enhancements to the C# language: - [Support for code generators](#support-for-code-generators) - [Module initializers](~/_csharplang/proposals/csharp-9.0/module-initializers.md) - [New features for partial methods](~/_csharplang/proposals/csharp-9.0/extending-partial-methods.md) -- [Warning wave 5](../language-reference/compiler-messages/warning-waves.md#warning-level-5) +- [Warning wave 5](../language-reference/compiler-messages/warning-waves.md#cs7023) C# 9.0 is supported on **.NET 5**. For more information, see [C# language versioning](../language-reference/configure-language-version.md). From bb2843e3906a1920e0a2e2967c6ba84608eac1a6 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 May 2022 18:24:28 -0400 Subject: [PATCH 08/12] build warnings part 2 --- .../language-reference/compiler-options/errors-warnings.md | 6 +++--- docs/csharp/whats-new/csharp-10.md | 2 +- docs/csharp/whats-new/csharp-11.md | 2 +- docs/csharp/whats-new/csharp-9.md | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index 85db475365ad7..ff693305ed591 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -55,9 +55,9 @@ To get information about an error or warning, you can look up the error code in | Analysis level | Meaning | |----------------|---------| -| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#cs7023). | -| 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#cs8826). | -| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#cs8981). | +| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#cs7023---a-static-type-is-used-in-an-is-or-as-expression). | +| 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#cs8826---partial-method-declarations-have-signature-differences). | +| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-character). | | latest | Displays all informational warnings up to and including the current release. | | preview | Displays all informational warnings up to and including the latest preview release. | | none | Turns off all informational warnings. | diff --git a/docs/csharp/whats-new/csharp-10.md b/docs/csharp/whats-new/csharp-10.md index 89817bd52a408..ce18f38c594cd 100644 --- a/docs/csharp/whats-new/csharp-10.md +++ b/docs/csharp/whats-new/csharp-10.md @@ -21,7 +21,7 @@ C# 10 adds the following features and enhancements to the C# language: - [Allow `AsyncMethodBuilder` attribute on methods](#allow-asyncmethodbuilder-attribute-on-methods) - [CallerArgumentExpression attribute](#callerargumentexpression-attribute-diagnostics) - [Enhanced `#line` pragma](#enhanced-line-pragma) -- [Warning wave 6](../language-reference/compiler-messages/warning-waves.md#cs8826) +- [Warning wave 6](../language-reference/compiler-messages/warning-waves.md#cs8826---partial-method-declarations-have-signature-differences) C# 10 is supported on **.NET 6**. For more information, see [C# language versioning](../language-reference/configure-language-version.md). diff --git a/docs/csharp/whats-new/csharp-11.md b/docs/csharp/whats-new/csharp-11.md index 6d5cec6fd3e74..a7ba8835541ad 100644 --- a/docs/csharp/whats-new/csharp-11.md +++ b/docs/csharp/whats-new/csharp-11.md @@ -18,7 +18,7 @@ The following features are available in the 6.0.200 version of the .NET SDK. The - [Newlines in string interpolation expressions](#newlines-in-string-interpolations). - [Improved method group conversion to delegate](#improved-method-group-conversion-to-delegate) - [Raw string literals](#raw-string-literals). -- [Warning wave 7](../language-reference/compiler-messages/warning-waves.md#cs7023) +- [Warning wave 7](../language-reference/compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-character) You can download the latest .NET 6 SDK from the [.NET downloads page](https://dotnet.microsoft.com/download). You can also download [Visual Studio 2022](https://visualstudio.microsoft.com/vs/), which includes the .NET 6 SDK. You can also try all these features with the preview release of the .NET 7 SDK, which can be downloaded from the [all .NET downloads](https://dotnet.microsoft.com/download/dotnet) page. diff --git a/docs/csharp/whats-new/csharp-9.md b/docs/csharp/whats-new/csharp-9.md index ba08cc7a4693b..9a8a7cfd1e5d1 100644 --- a/docs/csharp/whats-new/csharp-9.md +++ b/docs/csharp/whats-new/csharp-9.md @@ -26,7 +26,7 @@ C# 9.0 adds the following features and enhancements to the C# language: - [Support for code generators](#support-for-code-generators) - [Module initializers](~/_csharplang/proposals/csharp-9.0/module-initializers.md) - [New features for partial methods](~/_csharplang/proposals/csharp-9.0/extending-partial-methods.md) -- [Warning wave 5](../language-reference/compiler-messages/warning-waves.md#cs7023) +- [Warning wave 5](../language-reference/compiler-messages/warning-waves.md#cs7023---a-static-type-is-used-in-an-is-or-as-expression) C# 9.0 is supported on **.NET 5**. For more information, see [C# language versioning](../language-reference/configure-language-version.md). From 951a66292bb7ece9c18c44d85ded0a408790d7cd Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 11 May 2022 09:20:27 -0400 Subject: [PATCH 09/12] Add default analysis level. --- .../compiler-options/errors-warnings.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index ff693305ed591..3d1c5a57d7d91 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -53,14 +53,14 @@ To get information about an error or warning, you can look up the error code in ## Analysis level -| Analysis level | Meaning | -|----------------|---------| -| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#cs7023---a-static-type-is-used-in-an-is-or-as-expression). | -| 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#cs8826---partial-method-declarations-have-signature-differences). | -| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-character). | -| latest | Displays all informational warnings up to and including the current release. | -| preview | Displays all informational warnings up to and including the latest preview release. | -| none | Turns off all informational warnings. | +| Analysis level | Meaning | +|------------------|---------| +| 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#cs7023---a-static-type-is-used-in-an-is-or-as-expression). | +| 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#cs8826---partial-method-declarations-have-signature-differences). | +| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-character). | +| latest (default) | Displays all informational warnings up to and including the current release. | +| preview | Displays all informational warnings up to and including the latest preview release. | +| none | Turns off all informational warnings. | For more information on optional warnings, see [Warning waves](../compiler-messages/warning-waves.md).| From 6ccda74b785683397029242942aa9da171ace631 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 11 May 2022 11:35:37 -0400 Subject: [PATCH 10/12] Apply suggestions from code review Co-authored-by: Tom Dykstra --- .../compiler-messages/warning-waves.md | 16 ++++++++-------- .../compiler-options/errors-warnings.md | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/warning-waves.md b/docs/csharp/language-reference/compiler-messages/warning-waves.md index c6e391fa794ca..7296949914f91 100644 --- a/docs/csharp/language-reference/compiler-messages/warning-waves.md +++ b/docs/csharp/language-reference/compiler-messages/warning-waves.md @@ -41,15 +41,15 @@ helpviewer_keywords: New warnings and errors may 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 `true` 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. -## CS8981 - The type name only contains lower-cased ascii character +## CS8981 - The type name only contains lower-cased ascii characters. *Warning wave 7* -Any potential new keywords added for C# will be all lower-case ASCII characters. This warning ensures that none of your types conflict with potential future keywords. The following code produces CS8981: +Any new keywords added for C# will be all lower-case ASCII characters. This warning ensures that none of your types conflict with future keywords. The following code produces CS8981: :::code language="csharp" source="./snippets/WarningWaves/WaveSeven.cs" id="NoLowercaseTypes"::: -You can address this warning by renaming the type to include at least non-lower case ASCII character, such as an upper case character, a digit, or an underscore. +You can address this warning by renaming the type to include at least one non-lower case ASCII character, such as an upper case character, a digit, or an underscore. ## CS8826 - Partial method declarations have signature differences. @@ -68,7 +68,7 @@ The following partial class implementation generates several examples of CS8626: To fix any instance of these warnings, ensure the two signatures match. -## CS7023 - A static type is used in an `is` or `as` expression. +## CS7023 - A static type is used in an 'is' or 'as' expression. *Warning wave 5* @@ -82,7 +82,7 @@ The compiler reports this warning because the type test can never succeed. To co Console.WriteLine("o is not an instance of a static class"); ``` -## CS8073 - The result of the expression is always 'false' (or `true`). +## CS8073 - The result of the expression is always 'false' (or 'true'). *Warning wave 5* @@ -90,7 +90,7 @@ The `==` and `!=` operators always return `false` (or `true`) when comparing an :::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="StructsArentNull"::: -To fix this error, remove the null check, and code that would execute if the object is `null`. +To fix this error, remove the null check and code that would execute if the object is `null`. ## CS8848 - Operator 'from' can't be used here due to precedence. Use parentheses to disambiguate. @@ -120,7 +120,7 @@ The following examples show the warnings generated from the improved definite as - CS8883: Use of possibly unassigned auto-implemented property 'Property'. - CS8884: Use of possibly unassigned field 'Field' - CS8885: The 'this' object can't be used before all its fields have been assigned. -- CS8886: Use of unassigned output parameter `parameterName` +- CS8886: Use of unassigned output parameter 'parameterName'. - CS8887: Use of unassigned local variable 'variableName' :::code language="csharp" source="./snippets/WarningWaves/WaveFive.cs" id="DefiniteAssignmentWarnings"::: @@ -144,7 +144,7 @@ The following example generates CS8892: To fix this warning, remove or rename the asynchronous entry point. -## CS8897 - static types can't be used as parameters +## CS8897 - Static types can't be used as parameters *Warning wave 5* diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index 3d1c5a57d7d91..d17161bde04c7 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -62,7 +62,7 @@ To get information about an error or warning, you can look up the error code in | preview | Displays all informational warnings up to and including the latest preview release. | | none | Turns off all informational warnings. | -For more information on optional warnings, see [Warning waves](../compiler-messages/warning-waves.md).| +For more information on optional warnings, see [Warning waves](../compiler-messages/warning-waves.md). To get information about an error or warning, you can look up the error code in the Help Index. 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**](#disabledwarnings) to disable certain warnings. @@ -139,7 +139,7 @@ You can specify the SARIF format using the `version` argument to the `ErrorLog` logVersion21.json,version=2.1 ``` -The separator can be either a comma (`,`) or a semi-colon (`;`). Valid values are for version are: "1", "2", and "2.1". The default is "1". "2" and "2.1" both mean SARIF version 2.1.0. +The separator can be either a comma (`,`) or a semicolon (`;`). Valid values are for version are: "1", "2", and "2.1". The default is "1". "2" and "2.1" both mean SARIF version 2.1.0. ## ReportAnalyzer From 16ca985895a3995d0c04bee66413132da7c5183b Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 11 May 2022 11:41:03 -0400 Subject: [PATCH 11/12] Add a link to installing the offline help. --- .../language-reference/compiler-options/errors-warnings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index d17161bde04c7..78a237158e090 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -49,7 +49,7 @@ The element value is the warning level you want displayed for the compilation: L > [!WARNING] > 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. -To get information about an error or warning, you can look up the error code in the Help Index. 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**](#disabledwarnings) to disable certain warnings. +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**](#disabledwarnings) to disable certain warnings. ## Analysis level From 71b4d64ed9c3b7711537d435c370c10732dabcfa Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 11 May 2022 13:16:55 -0400 Subject: [PATCH 12/12] fix warning --- .../language-reference/compiler-options/errors-warnings.md | 2 +- docs/csharp/whats-new/csharp-11.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/compiler-options/errors-warnings.md b/docs/csharp/language-reference/compiler-options/errors-warnings.md index 78a237158e090..62a19158527a7 100644 --- a/docs/csharp/language-reference/compiler-options/errors-warnings.md +++ b/docs/csharp/language-reference/compiler-options/errors-warnings.md @@ -57,7 +57,7 @@ To get information about an error or warning, you can look up the error code in |------------------|---------| | 5 | Displays all optional [warning wave 5 warnings](../compiler-messages/warning-waves.md#cs7023---a-static-type-is-used-in-an-is-or-as-expression). | | 6 | Displays all optional [warning wave 6 warnings](../compiler-messages/warning-waves.md#cs8826---partial-method-declarations-have-signature-differences). | -| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-character). | +| 7 | Displays all optional [warning wave 7 warnings](../compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-characters). | | latest (default) | Displays all informational warnings up to and including the current release. | | preview | Displays all informational warnings up to and including the latest preview release. | | none | Turns off all informational warnings. | diff --git a/docs/csharp/whats-new/csharp-11.md b/docs/csharp/whats-new/csharp-11.md index a7ba8835541ad..62b95c475a4f5 100644 --- a/docs/csharp/whats-new/csharp-11.md +++ b/docs/csharp/whats-new/csharp-11.md @@ -18,7 +18,7 @@ The following features are available in the 6.0.200 version of the .NET SDK. The - [Newlines in string interpolation expressions](#newlines-in-string-interpolations). - [Improved method group conversion to delegate](#improved-method-group-conversion-to-delegate) - [Raw string literals](#raw-string-literals). -- [Warning wave 7](../language-reference/compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-character) +- [Warning wave 7](../language-reference/compiler-messages/warning-waves.md#cs8981---the-type-name-only-contains-lower-cased-ascii-characters) You can download the latest .NET 6 SDK from the [.NET downloads page](https://dotnet.microsoft.com/download). You can also download [Visual Studio 2022](https://visualstudio.microsoft.com/vs/), which includes the .NET 6 SDK. You can also try all these features with the preview release of the .NET 7 SDK, which can be downloaded from the [all .NET downloads](https://dotnet.microsoft.com/download/dotnet) page.