未验证 提交 40bb50e8 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #22100 from sharwell/fix-warning-level

Make analyzer diagnostics independent of the current warning level
......@@ -120,12 +120,12 @@ public void Goo()
var hidden = diag.WithSeverity(DiagnosticSeverity.Hidden);
Assert.Equal(DiagnosticSeverity.Hidden, hidden.Severity);
Assert.Equal(DiagnosticSeverity.Warning, hidden.DefaultSeverity);
Assert.Equal(4, hidden.WarningLevel);
Assert.Equal(1, hidden.WarningLevel);
var info = diag.WithSeverity(DiagnosticSeverity.Info);
Assert.Equal(DiagnosticSeverity.Info, info.Severity);
Assert.Equal(DiagnosticSeverity.Warning, info.DefaultSeverity);
Assert.Equal(4, info.WarningLevel);
Assert.Equal(1, info.WarningLevel);
}
[Fact, WorkItem(7446, "https://github.com/dotnet/roslyn/issues/7446")]
......
......@@ -512,6 +512,19 @@ internal Diagnostic WithReportDiagnostic(ReportDiagnostic reportAction)
}
}
/// <summary>
/// Gets the default warning level for a diagnostic severity. Warning levels are used with the <c>/warn:N</c>
/// command line option to suppress diagnostics over a severity of interest. When N is 0, only error severity
/// messages are produced by the compiler. Values greater than 0 indicated that warnings up to and including
/// level N should also be included.
/// </summary>
/// <remarks>
/// <see cref="DiagnosticSeverity.Info"/> and <see cref="DiagnosticSeverity.Hidden"/> are treated as warning
/// level 1. In other words, these diagnostics which typically interact with editor features are enabled unless
/// the special <c>/warn:0</c> option is set.
/// </remarks>
/// <param name="severity">A <see cref="DiagnosticSeverity"/> value.</param>
/// <returns>The default compiler warning level for <paramref name="severity"/>.</returns>
internal static int GetDefaultWarningLevel(DiagnosticSeverity severity)
{
switch (severity)
......@@ -520,10 +533,8 @@ internal static int GetDefaultWarningLevel(DiagnosticSeverity severity)
return 0;
case DiagnosticSeverity.Warning:
return 1;
default:
return HighestValidWarningLevel;
return 1;
}
}
......
......@@ -112,12 +112,12 @@ End Class
Dim hidden = warning.WithSeverity(DiagnosticSeverity.Hidden)
Assert.Equal(DiagnosticSeverity.Hidden, hidden.Severity)
Assert.Equal(DiagnosticSeverity.Warning, hidden.DefaultSeverity)
Assert.Equal(4, hidden.WarningLevel)
Assert.Equal(1, hidden.WarningLevel)
Dim info = warning.WithSeverity(DiagnosticSeverity.Info)
Assert.Equal(DiagnosticSeverity.Info, info.Severity)
Assert.Equal(DiagnosticSeverity.Warning, info.DefaultSeverity)
Assert.Equal(4, info.WarningLevel)
Assert.Equal(1, info.WarningLevel)
End Sub
<Fact, WorkItem(7446, "https://github.com/dotnet/roslyn/issues/7446")>
......
......@@ -3995,6 +3995,37 @@ void Main()
options: PreferIntrinsicTypeInMemberAccess);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyTypeNames)]
[WorkItem(20377, "https://github.com/dotnet/roslyn/issues/20377")]
public async Task TestWarningLevel(int warningLevel)
{
await TestInRegularAndScriptAsync(
@"using System;
namespace Root
{
class A
{
[|System.Exception|] c;
}
}",
@"using System;
namespace Root
{
class A
{
Exception c;
}
}", compilationOptions: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, warningLevel: warningLevel));
}
private async Task TestWithPredefinedTypeOptionsAsync(string code, string expected, int index = 0)
{
await TestInRegularAndScriptAsync(code, expected, index: index, options: PreferIntrinsicTypeEverywhere);
......@@ -4004,6 +4035,10 @@ private async Task TestWithPredefinedTypeOptionsAsync(string code, string expect
SingleOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, true, NotificationOption.Error),
SingleOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, this.onWithError, GetLanguage()));
private IDictionary<OptionKey, object> PreferIntrinsicTypeEverywhereAsWarning => OptionsSet(
SingleOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, true, NotificationOption.Warning),
SingleOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, this.onWithWarning, GetLanguage()));
private IDictionary<OptionKey, object> PreferIntrinsicTypeInDeclaration => OptionsSet(
SingleOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, true, NotificationOption.Error),
SingleOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, this.offWithNone, GetLanguage()));
......
......@@ -1353,5 +1353,34 @@ static void Main(string[] args)
}
}");
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryImports)]
[WorkItem(20377, "https://github.com/dotnet/roslyn/issues/20377")]
public async Task TestWarningLevel(int warningLevel)
{
await TestInRegularAndScriptAsync(
@"[|using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
}
}|]",
@"class Program
{
static void Main(string[] args)
{
}
}", compilationOptions: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, warningLevel: warningLevel));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册