Skip to content

Commit e3a9be0

Browse files
authored
Merge pull request #56299 from dotnet/merges/main-to-release/dev17.1-preview1
Merge main to release/dev17.1-preview1
2 parents ff43d2f + 503766c commit e3a9be0

File tree

84 files changed

+959
-517
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+959
-517
lines changed

src/Analyzers/CSharp/Analyzers/AddBraces/CSharpAddBracesDiagnosticAnalyzer.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Diagnostics;
86
using System.Threading;
97
using Microsoft.CodeAnalysis.CodeStyle;
108
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
119
using Microsoft.CodeAnalysis.CSharp.Extensions;
1210
using Microsoft.CodeAnalysis.CSharp.Syntax;
1311
using Microsoft.CodeAnalysis.Diagnostics;
12+
using Microsoft.CodeAnalysis.Shared.Extensions;
1413
using Microsoft.CodeAnalysis.Text;
14+
using Roslyn.Utilities;
1515
using FormattingRangeHelper = Microsoft.CodeAnalysis.CSharp.Utilities.FormattingRangeHelper;
1616

1717
namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces
@@ -57,6 +57,8 @@ public void AnalyzeNode(SyntaxNodeAnalysisContext context)
5757
}
5858

5959
var embeddedStatement = statement.GetEmbeddedStatement();
60+
Contract.ThrowIfNull(embeddedStatement);
61+
6062
switch (embeddedStatement.Kind())
6163
{
6264
case SyntaxKind.Block:
@@ -121,7 +123,7 @@ public void AnalyzeNode(SyntaxNodeAnalysisContext context)
121123
/// </summary>
122124
private static bool ContainsInterleavedDirective(SyntaxNode statement, StatementSyntax embeddedStatement, CancellationToken cancellationToken)
123125
{
124-
if (statement.IsKind(SyntaxKind.IfStatement, out IfStatementSyntax ifStatementNode))
126+
if (statement.IsKind(SyntaxKind.IfStatement, out IfStatementSyntax? ifStatementNode))
125127
{
126128
var elseNode = ifStatementNode.Else;
127129
if (elseNode != null && !embeddedStatement.IsMissing)
@@ -265,23 +267,16 @@ private static IfStatementSyntax GetOutermostIfStatementOfSequence(SyntaxNode if
265267
IfStatementSyntax result;
266268
if (ifStatementOrElseClause.IsKind(SyntaxKind.ElseClause))
267269
{
268-
result = (IfStatementSyntax)ifStatementOrElseClause.Parent;
270+
result = (IfStatementSyntax)ifStatementOrElseClause.GetRequiredParent();
269271
}
270272
else
271273
{
272274
Debug.Assert(ifStatementOrElseClause.IsKind(SyntaxKind.IfStatement));
273275
result = (IfStatementSyntax)ifStatementOrElseClause;
274276
}
275277

276-
while (result != null)
277-
{
278-
if (!result.IsParentKind(SyntaxKind.ElseClause))
279-
{
280-
break;
281-
}
282-
283-
result = (IfStatementSyntax)result.Parent.Parent;
284-
}
278+
while (result.IsParentKind(SyntaxKind.ElseClause))
279+
result = (IfStatementSyntax)result.GetRequiredParent().GetRequiredParent();
285280

286281
return result;
287282
}
@@ -290,21 +285,17 @@ private static IfStatementSyntax GetOutermostIfStatementOfSequence(SyntaxNode if
290285
/// Determines if any embedded statement of an <c>if</c>/<c>else if</c>/<c>else</c> sequence uses braces. Only
291286
/// the embedded statements falling <em>immediately</em> under one of these nodes are checked.
292287
/// </summary>
293-
private static bool AnyPartOfIfSequenceUsesBraces(IfStatementSyntax statement)
288+
private static bool AnyPartOfIfSequenceUsesBraces(IfStatementSyntax? statement)
294289
{
295290
// Iterative instead of recursive to avoid stack depth problems
296291
while (statement != null)
297292
{
298293
if (statement.Statement.IsKind(SyntaxKind.Block))
299-
{
300294
return true;
301-
}
302295

303296
var elseStatement = statement.Else?.Statement;
304297
if (elseStatement.IsKind(SyntaxKind.Block))
305-
{
306298
return true;
307-
}
308299

309300
statement = elseStatement as IfStatementSyntax;
310301
}

src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionConstants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
namespace Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression
86
{
97
internal static class ConvertSwitchStatementToExpressionConstants

src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionDiagnosticAnalyzer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Immutable;
86
using System.Globalization;
97
using System.Linq;
@@ -80,7 +78,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
8078
location: switchStatement.GetFirstToken().GetLocation(),
8179
effectiveSeverity: styleOption.Notification.Severity,
8280
additionalLocations: additionalLocations.ToArrayAndFree(),
83-
properties: ImmutableDictionary<string, string>.Empty
81+
properties: ImmutableDictionary<string, string?>.Empty
8482
.Add(Constants.NodeToGenerateKey, ((int)nodeToGenerate).ToString(CultureInfo.InvariantCulture))
8583
.Add(Constants.ShouldRemoveNextStatementKey, shouldRemoveNextStatement.ToString(CultureInfo.InvariantCulture))));
8684
}

src/Analyzers/CSharp/Analyzers/ConvertSwitchStatementToExpression/ConvertSwitchStatementToExpressionHelpers.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using Microsoft.CodeAnalysis.CSharp.Extensions;
86
using Microsoft.CodeAnalysis.CSharp.Syntax;
97

@@ -19,7 +17,7 @@ public static bool IsDefaultSwitchLabel(SwitchLabelSyntax node)
1917
return true;
2018
}
2119

22-
if (node.IsKind(SyntaxKind.CasePatternSwitchLabel, out CasePatternSwitchLabelSyntax @case))
20+
if (node.IsKind(SyntaxKind.CasePatternSwitchLabel, out CasePatternSwitchLabelSyntax? @case))
2321
{
2422
// case _:
2523
if (@case.Pattern.IsKind(SyntaxKind.DiscardPattern))
@@ -29,7 +27,7 @@ public static bool IsDefaultSwitchLabel(SwitchLabelSyntax node)
2927

3028
// case var _:
3129
// case var x:
32-
if (@case.Pattern.IsKind(SyntaxKind.VarPattern, out VarPatternSyntax varPattern) &&
30+
if (@case.Pattern.IsKind(SyntaxKind.VarPattern, out VarPatternSyntax? varPattern) &&
3331
varPattern.Designation.IsKind(SyntaxKind.DiscardDesignation, SyntaxKind.SingleVariableDesignation))
3432
{
3533
return @case.WhenClause == null;

src/Analyzers/CSharp/Analyzers/InvokeDelegateWithConditionalAccess/InvokeDelegateWithConditionalAccessAnalyzer.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Immutable;
86
using Microsoft.CodeAnalysis.CodeStyle;
97
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
@@ -74,7 +72,7 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
7472

7573
// Check for both: "if (...) { a(); }" and "if (...) a();"
7674
var innerStatement = ifStatement.Statement;
77-
if (innerStatement.IsKind(SyntaxKind.Block, out BlockSyntax block))
75+
if (innerStatement.IsKind(SyntaxKind.Block, out BlockSyntax? block))
7876
{
7977
if (block.Statements.Count != 1)
8078
{
@@ -84,13 +82,13 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
8482
innerStatement = block.Statements[0];
8583
}
8684

87-
if (!innerStatement.IsKind(SyntaxKind.ExpressionStatement, out ExpressionStatementSyntax expressionStatement))
85+
if (!innerStatement.IsKind(SyntaxKind.ExpressionStatement, out ExpressionStatementSyntax? expressionStatement))
8886
{
8987
return;
9088
}
9189

9290
// Check that it's of the form: "if (a != null) { a(); }
93-
if (!(expressionStatement.Expression is InvocationExpressionSyntax invocationExpression))
91+
if (expressionStatement.Expression is not InvocationExpressionSyntax invocationExpression)
9492
{
9593
return;
9694
}
@@ -162,7 +160,7 @@ private void ReportDiagnostics(
162160
{
163161
var tree = syntaxContext.Node.SyntaxTree;
164162

165-
var properties = ImmutableDictionary<string, string>.Empty.Add(
163+
var properties = ImmutableDictionary<string, string?>.Empty.Add(
166164
Constants.Kind, kind);
167165

168166
var previousToken = expressionStatement.GetFirstToken().GetPreviousToken();
@@ -247,7 +245,7 @@ private bool TryCheckVariableAndIfStatementForm(
247245
}
248246

249247
var previousStatement = parentBlock.Statements[ifIndex - 1];
250-
if (!previousStatement.IsKind(SyntaxKind.LocalDeclarationStatement, out LocalDeclarationStatementSyntax localDeclarationStatement))
248+
if (!previousStatement.IsKind(SyntaxKind.LocalDeclarationStatement, out LocalDeclarationStatementSyntax? localDeclarationStatement))
251249
{
252250
return false;
253251
}
@@ -288,16 +286,14 @@ private bool TryCheckVariableAndIfStatementForm(
288286
return false;
289287
}
290288

291-
var localSymbol = (ILocalSymbol)semanticModel.GetDeclaredSymbol(declarator, cancellationToken);
289+
var localSymbol = (ILocalSymbol)semanticModel.GetRequiredDeclaredSymbol(declarator, cancellationToken);
292290

293291
// Ok, we made a local just to check it for null and invoke it. Looks like something
294292
// we can suggest an improvement for!
295293
// But first make sure we're only using the local only within the body of this if statement.
296294
var analysis = semanticModel.AnalyzeDataFlow(localDeclarationStatement, ifStatement);
297-
if (analysis.ReadOutside.Contains(localSymbol) || analysis.WrittenOutside.Contains(localSymbol))
298-
{
295+
if (analysis == null || analysis.ReadOutside.Contains(localSymbol) || analysis.WrittenOutside.Contains(localSymbol))
299296
return false;
300-
}
301297

302298
// Looks good!
303299
var tree = semanticModel.SyntaxTree;

src/Analyzers/CSharp/Analyzers/MakeStructFieldsWritable/CSharpMakeStructFieldsWritableDiagnosticAnalyzer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Immutable;
86
using System.Linq;
97
using System.Threading;

src/Analyzers/CSharp/Analyzers/OrderModifiers/CSharpOrderModifiersDiagnosticAnalyzer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Generic;
86
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
97
using Microsoft.CodeAnalysis.CSharp.LanguageServices;

src/Analyzers/CSharp/Analyzers/OrderModifiers/CSharpOrderModifiersHelper.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
87
using Microsoft.CodeAnalysis.OrderModifiers;
98

109
namespace Microsoft.CodeAnalysis.CSharp.OrderModifiers
@@ -23,12 +22,10 @@ protected override int GetKeywordKind(string trimmed)
2322
return (int)(kind == SyntaxKind.None ? SyntaxFacts.GetContextualKeywordKind(trimmed) : kind);
2423
}
2524

26-
protected override bool TryParse(string value, out Dictionary<int, int> parsed)
25+
protected override bool TryParse(string value, [NotNullWhen(true)] out Dictionary<int, int>? parsed)
2726
{
2827
if (!base.TryParse(value, out parsed))
29-
{
3028
return false;
31-
}
3229

3330
// 'partial' must always go at the end in C#.
3431
parsed[(int)SyntaxKind.PartialKeyword] = int.MaxValue;

src/Analyzers/CSharp/Analyzers/QualifyMemberAccess/CSharpQualifyMemberAccessDiagnosticAnalyzer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Linq;
86
using Microsoft.CodeAnalysis.CSharp.Extensions;
97
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -40,7 +38,7 @@ protected override bool CanMemberAccessBeQualified(ISymbol containingSymbol, Syn
4038
}
4139

4240
return !(node.IsKind(SyntaxKind.BaseExpression) ||
43-
node.Parent.Parent.IsKind(SyntaxKind.ObjectInitializerExpression) ||
41+
node.GetRequiredParent().GetRequiredParent().IsKind(SyntaxKind.ObjectInitializerExpression) ||
4442
IsInPropertyOrFieldInitialization(containingSymbol, node));
4543
}
4644

src/Analyzers/CSharp/Analyzers/RemoveUnreachableCode/CSharpRemoveUnreachableCodeDiagnosticAnalyzer.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections.Immutable;
86
using System.Linq;
97
using Microsoft.CodeAnalysis.CodeStyle;
@@ -22,7 +20,7 @@ internal class CSharpRemoveUnreachableCodeDiagnosticAnalyzer : AbstractBuiltInCo
2220
private const string CS0162 = nameof(CS0162); // Unreachable code detected
2321

2422
public const string IsSubsequentSection = nameof(IsSubsequentSection);
25-
private static readonly ImmutableDictionary<string, string> s_subsequentSectionProperties = ImmutableDictionary<string, string>.Empty.Add(IsSubsequentSection, "");
23+
private static readonly ImmutableDictionary<string, string?> s_subsequentSectionProperties = ImmutableDictionary<string, string?>.Empty.Add(IsSubsequentSection, "");
2624

2725
public CSharpRemoveUnreachableCodeDiagnosticAnalyzer()
2826
: base(IDEDiagnosticIds.RemoveUnreachableCodeDiagnosticId,
@@ -148,8 +146,13 @@ private void ProcessUnreachableDiagnostic(
148146
additionalUnnecessaryLocations = ImmutableArray.Create(location);
149147
}
150148

151-
context.ReportDiagnostic(
152-
DiagnosticHelper.CreateWithLocationTags(Descriptor, location, ReportDiagnostic.Default, additionalLocations, additionalUnnecessaryLocations, s_subsequentSectionProperties));
149+
context.ReportDiagnostic(DiagnosticHelper.CreateWithLocationTags(
150+
Descriptor,
151+
location,
152+
ReportDiagnostic.Default,
153+
additionalLocations,
154+
additionalUnnecessaryLocations,
155+
s_subsequentSectionProperties));
153156
}
154157
}
155158
}

0 commit comments

Comments
 (0)