提交 883ffdb8 编写于 作者: M Manish Vasani

Enable and fix IDE0007 violations (Prefer "var")

上级 c54743b7
......@@ -300,4 +300,10 @@ dotnet_diagnostic.IDE0051.severity = warning
dotnet_diagnostic.IDE0052.severity = warning
# IDE0059: Unnecessary assignment to a value
dotnet_diagnostic.IDE0059.severity = warning
\ No newline at end of file
dotnet_diagnostic.IDE0059.severity = warning
# Prefer "var" everywhere
dotnet_diagnostic.IDE0007.severity = warning
csharp_style_var_for_built_in_types = true:warning
csharp_style_var_when_type_is_apparent = true:warning
csharp_style_var_elsewhere = true:warning
\ No newline at end of file
......@@ -28,14 +28,14 @@ public CSharpSendToInteractiveSubmissionProvider()
protected override bool CanParseSubmission(string code)
{
ParseOptions options = CSharpParseOptions.Default.WithKind(SourceCodeKind.Script);
SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(code, options);
var tree = SyntaxFactory.ParseSyntaxTree(code, options);
return tree.HasCompilationUnitRoot &&
!tree.GetDiagnostics().Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error);
}
protected override IEnumerable<TextSpan> GetExecutableSyntaxTreeNodeSelection(TextSpan selectionSpan, SyntaxNode root)
{
SyntaxNode expandedNode = GetSyntaxNodeForSubmission(selectionSpan, root);
var expandedNode = GetSyntaxNodeForSubmission(selectionSpan, root);
return expandedNode != null
? new TextSpan[] { expandedNode.Span }
: Array.Empty<TextSpan>();
......
......@@ -4190,7 +4190,7 @@ IEnumerable<int> M(IEnumerable<int> nums)
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertQueryToForEach)]
public async Task EnumerableFunctionDoesNotUseLocalFunctionName()
{
string source = @"
var source = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4204,7 +4204,7 @@ public static void Main(string[] args)
void enumerable() { }
}
}";
string output = @"
var output = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4232,7 +4232,7 @@ IEnumerable<int> enumerable1()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertQueryToForEach)]
public async Task EnumerableFunctionCanUseLocalFunctionParameterName()
{
string source = @"
var source = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4246,7 +4246,7 @@ public static void Main(string[] args)
void M(IEnumerable<int> enumerable) { }
}
}";
string output = @"
var output = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4274,7 +4274,7 @@ IEnumerable<int> enumerable()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertQueryToForEach)]
public async Task EnumerableFunctionDoesNotUseLambdaParameterNameWithCSharpLessThan8()
{
string source = @"
var source = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4288,7 +4288,7 @@ public static void Main(string[] args)
Action<int> myLambda = enumerable => { };
}
}";
string output = @"
var output = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4316,7 +4316,7 @@ IEnumerable<int> enumerable1()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertQueryToForEach)]
public async Task EnumerableFunctionCanUseLambdaParameterNameInCSharp8()
{
string source = @"
var source = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4330,7 +4330,7 @@ public static void Main(string[] args)
Action<int> myLambda = enumerable => { };
}
}";
string output = @"
var output = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4362,7 +4362,7 @@ IEnumerable<int> enumerable()
[WorkItem(35180, "https://github.com/dotnet/roslyn/issues/35180")]
public async Task DeclarationSelection()
{
string source = @"
var source = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4374,7 +4374,7 @@ public static void Main(string[] args)
var r = [|from i in c select i+1;|]
}
}";
string output = @"
var output = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4401,7 +4401,7 @@ IEnumerable<int> enumerable()
[WorkItem(35180, "https://github.com/dotnet/roslyn/issues/35180")]
public async Task LocalAssignmentSelection()
{
string source = @"
var source = @"
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -4414,7 +4414,7 @@ public static void Main(string[] args)
[|r = from i in c select i+1;|]
}
}";
string output = @"
var output = @"
using System;
using System.Collections.Generic;
using System.Linq;
......
......@@ -1020,7 +1020,7 @@ namespace Test
class Program { }
}";
string expectedCodeAfterCommit = @"
var expectedCodeAfterCommit = @"
using Foo;
namespace Test
......@@ -1082,7 +1082,7 @@ class Program
}
}";
string expectedCodeAfterCommit = @"
var expectedCodeAfterCommit = @"
using Foo;
namespace Test
......@@ -1141,7 +1141,7 @@ namespace Test
class Program { }
}";
string expectedCodeAfterCommit = @"
var expectedCodeAfterCommit = @"
using Foo;
namespace Test
......@@ -1203,7 +1203,7 @@ class Program
}
}";
string expectedCodeAfterCommit = @"
var expectedCodeAfterCommit = @"
using Foo;
namespace Test
......
......@@ -624,7 +624,7 @@ public async Task TestNuGetAndVsixAnalyzer_MultipleNuGetAnalyzersCollectivelyRep
diagnostics.Verify(expectedDiagnostics.Select(d => d.diagnostic).ToArray());
int index = 0;
var index = 0;
foreach (var (_, expectedMessage) in expectedDiagnostics)
{
Assert.Equal(expectedMessage, diagnostics[index].GetMessage());
......
......@@ -4087,7 +4087,7 @@ static unsafe void Main(string[] args)
[Fact]
public void ForEach_Update_Nullable()
{
string src1 = @"
var src1 = @"
class C
{
static void F()
......@@ -4100,7 +4100,7 @@ static void F()
}
}
";
string src2 = @"
var src2 = @"
class C
{
static void F()
......@@ -4122,7 +4122,7 @@ static void F()
[Fact]
public void ForEach_DeleteBody()
{
string src1 = @"
var src1 = @"
class C
{
static void F()
......@@ -4131,7 +4131,7 @@ static void F()
}
}
";
string src2 = @"
var src2 = @"
class C
{
static void F()
......@@ -4149,7 +4149,7 @@ static void F()
[Fact]
public void ForEachVariable_DeleteBody()
{
string src1 = @"
var src1 = @"
class C
{
static void F()
......@@ -4158,7 +4158,7 @@ static void F()
}
}
";
string src2 = @"
var src2 = @"
class C
{
static void F()
......@@ -5442,7 +5442,7 @@ public static void Main()
[Fact]
public void DoWhileBody_Delete()
{
string src1 = @"
var src1 = @"
class C
{
static void F()
......@@ -5451,7 +5451,7 @@ static void F()
}
}
";
string src2 = @"
var src2 = @"
class C
{
static void F()
......@@ -10437,7 +10437,7 @@ static void F(object o1, object o2)
[Fact]
public void ChangeLocalNullableToNonNullable()
{
string src1 = @"
var src1 = @"
class C
{
static void F()
......@@ -10446,7 +10446,7 @@ static void F()
}
}
";
string src2 = @"
var src2 = @"
class C
{
static void F()
......@@ -10464,7 +10464,7 @@ static void F()
[Fact]
public void ChangeLocalNonNullableToNullable()
{
string src1 = @"
var src1 = @"
class C
{
static void F()
......@@ -10473,7 +10473,7 @@ static void F()
}
}
";
string src2 = @"
var src2 = @"
class C
{
static void F()
......@@ -10615,7 +10615,7 @@ public static void F()
[Fact]
public void Block_Delete()
{
string src1 = @"
var src1 = @"
class C
{
public static void F()
......@@ -10626,7 +10626,7 @@ public static void F()
}
}
";
string src2 = @"
var src2 = @"
class C
{
public static void F()
......
......@@ -74,7 +74,7 @@ public static IEnumerable<TextSpan> GetBreakpointSequence(SyntaxNode root, int p
{
TextSpan lastSpan = default;
var endPosition = root.Span.End;
for (int p = position; p < endPosition; p++)
for (var p = position; p < endPosition; p++)
{
if (BreakpointSpans.TryGetClosestBreakpointSpan(root, p, out var span) && span.Start > lastSpan.Start)
{
......
......@@ -2847,14 +2847,14 @@ static void Main(string[] b)
[Fact]
public void MethodUpdate_UpdateParameterToNullable()
{
string src1 = @"
var src1 = @"
class C
{
static void M(string s)
{
}
}";
string src2 = @"
var src2 = @"
class C
{
static void M(string? s)
......@@ -2873,7 +2873,7 @@ static void M(string? s)
[Fact]
public void MethodUpdate_UpdateParameterToNonNullable()
{
string src1 = @"
var src1 = @"
class C
{
static void M(string? s)
......@@ -2881,7 +2881,7 @@ static void M(string? s)
}
}";
string src2 = @"
var src2 = @"
class C
{
static void M(string s)
......
......@@ -33,7 +33,7 @@ protected override async Task TestWorkerAsync(Document document, TextSpan textSp
if (mode == TestMode.Position)
{
int position = node?.SpanStart ?? textSpan.Start;
var position = node?.SpanStart ?? textSpan.Start;
inferredType = typeInference.InferType(await document.ReuseExistingSpeculativeModelAsync(position, CancellationToken.None), position, objectAsDefault: true, cancellationToken: CancellationToken.None);
}
else
......
......@@ -205,11 +205,11 @@ public async Task<ImmutableArray<CodeFixCollection>> GetFixesAsync(Document docu
if (result.Count > 0 && _fixerPriorityMap.TryGetValue(document.Project.Language, out var fixersForLanguage))
{
// sort the result to the order defined by the fixers
ImmutableDictionary<CodeFixProvider, int> priorityMap = fixersForLanguage.Value;
var priorityMap = fixersForLanguage.Value;
result.Sort((d1, d2) => GetValue(d1).CompareTo(GetValue(d2)));
int GetValue(CodeFixCollection c)
=> priorityMap.TryGetValue((CodeFixProvider)c.Provider, out var value) ? value : int.MaxValue;
=> priorityMap!.TryGetValue((CodeFixProvider)c.Provider, out var value) ? value : int.MaxValue;
}
// TODO (https://github.com/dotnet/roslyn/issues/4932): Don't restrict CodeFixes in Interactive
......
......@@ -206,7 +206,7 @@ private async Task TrackActiveSpansAsync(CancellationToken cancellationToken)
lock (_trackingSpans)
{
for (int i = 0; i < baseActiveStatementSpans.Length; i++)
for (var i = 0; i < baseActiveStatementSpans.Length; i++)
{
var document = currentSolution.GetDocument(openDocumentIds[i]);
if (document == null)
......@@ -327,7 +327,7 @@ internal async Task<ImmutableArray<ActiveStatementTrackingSpan>> GetLatestSpansA
lock (_trackingSpans)
{
bool hasExistingSpans = _trackingSpans.TryGetValue(document.Id, out var oldSpans);
var hasExistingSpans = _trackingSpans.TryGetValue(document.Id, out var oldSpans);
if (activeStatementSpans.IsDefault)
{
......
......@@ -27,7 +27,7 @@ internal static class InferredIndentationOptions
if (snapshot != null)
{
indentationManagerService.GetIndentation(snapshot.TextBuffer, explicitFormat, out bool convertTabsToSpaces, out int tabSize, out int indentSize);
indentationManagerService.GetIndentation(snapshot.TextBuffer, explicitFormat, out var convertTabsToSpaces, out var tabSize, out var indentSize);
options = options.WithChangedOption(FormattingOptions.UseTabs, !convertTabsToSpaces)
.WithChangedOption(FormattingOptions.IndentationSize, indentSize)
......
......@@ -76,7 +76,7 @@ public class IDEDiagnosticIDConfigurationTests
expectedLines = expected.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Assert.True(expectedLines.Length % 2 == 0);
var expectedMap = new Dictionary<string, string>();
for (int i = 0; i < expectedLines.Length; i += 2)
for (var i = 0; i < expectedLines.Length; i += 2)
{
expectedMap.Add(expectedLines[i].Trim(), expectedLines[i + 1].Trim());
}
......
......@@ -51,7 +51,7 @@ public static ISymUnmanagedReader5 OpenDummySymReader(ImmutableArray<byte> pdbIm
var pdbStreamCom = SymUnmanagedStreamFactory.CreateStream(pdbStream);
if (pdbImage.Length > 4 && pdbImage[0] == 'B' && pdbImage[1] == 'S' && pdbImage[2] == 'J' && pdbImage[3] == 'B')
{
int hr = symBinder.GetReaderFromPdbStream(metadataImportProvider, pdbStreamCom, out var symReader);
var hr = symBinder.GetReaderFromPdbStream(metadataImportProvider, pdbStreamCom, out var symReader);
Assert.Equal(0, hr);
return (ISymUnmanagedReader5)symReader;
}
......
......@@ -765,7 +765,7 @@ private static CompilationOptions CreateCompilationOptions(TestWorkspace workspa
HostLanguageServices languageServiceProvider,
ImmutableArray<string> roles)
{
string markupCode = documentElement.NormalizedValue();
var markupCode = documentElement.NormalizedValue();
var folders = GetFolders(documentElement);
var optionsElement = documentElement.Element(ParseOptionsElementName);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册