Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable IDE2xxx experimental rules #6353

Merged
merged 10 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ dotnet_diagnostic.IDE0130.severity = silent
# IDE0150: Null check can be clarified
dotnet_diagnostic.IDE0150.severity = silent

# IDE2000: Allow multiple blank lines
dotnet_style_allow_multiple_blank_lines_experimental = false

# IDE2001: Embedded statements must be on their own line
csharp_style_allow_embedded_statements_on_same_line_experimental = false

# IDE2002: Consecutive braces must not have blank line between them
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false

# IDE2003: Blank line required between block and subsequent statement
dotnet_style_allow_statement_immediately_after_block_experimental = false

# IDE2004: Blank line not allowed after constructor initializer colon
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = false

### Configuration for FxCop analyzers executed on this repo ###
[*.{cs,vb}]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private static void HandleInvocationOperation(
{
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(GetHashCodeRule));
}

break;

case s_symbolEqualsName:
Expand All @@ -176,6 +177,7 @@ private static void HandleInvocationOperation(
context.ReportDiagnostic(invocationOperation.Syntax.GetLocation().CreateDiagnostic(EqualityRule));
}
}

break;

case s_HashCodeCombineName:
Expand All @@ -187,6 +189,7 @@ invocationOperation.Instance is null &&
{
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(GetHashCodeRule));
}

break;

default:
Expand All @@ -198,6 +201,7 @@ invocationOperation.Instance is null &&
{
context.ReportDiagnostic(invocationOperation.CreateDiagnostic(CollectionRule));
}

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
cancellationToken => UpdateEntryInUnshippedFileAsync(context.Document.Project, ruleId, entryToUpdate, cancellationToken),
equivalenceKey: CodeAnalysisDiagnosticsResources.UpdateEntryForDiagnosticIdInAnalyzerReleaseCodeFixTitle);
}

break;

case DiagnosticIds.EnableAnalyzerReleaseTrackingRuleId:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Analyzers.MetaAnalyzers.CodeFixes
If invocation Is Nothing Then
Return binaryExpression
End If

Dim newInvocation = invocation _
.WithExpression(ConvertKindNameToIsKind(invocation.Expression)) _
.AddArgumentListArguments(SyntaxFactory.SimpleArgument(binaryExpression.Right.WithoutTrailingTrivia())) _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte
VerifySymbol(context.ReportDiagnostic, conversion.OperatorMethod, context.Operation.Syntax);
VerifyType(context.ReportDiagnostic, conversion.OperatorMethod.ContainingType, context.Operation.Syntax);
}

break;

case IUnaryOperation unary:
Expand All @@ -109,6 +110,7 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte
VerifySymbol(context.ReportDiagnostic, unary.OperatorMethod, context.Operation.Syntax);
VerifyType(context.ReportDiagnostic, unary.OperatorMethod.ContainingType, context.Operation.Syntax);
}

break;

case IBinaryOperation binary:
Expand All @@ -117,6 +119,7 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte
VerifySymbol(context.ReportDiagnostic, binary.OperatorMethod, context.Operation.Syntax);
VerifyType(context.ReportDiagnostic, binary.OperatorMethod.ContainingType, context.Operation.Syntax);
}

break;

case IIncrementOrDecrementOperation incrementOrDecrement:
Expand All @@ -125,6 +128,7 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte
VerifySymbol(context.ReportDiagnostic, incrementOrDecrement.OperatorMethod, context.Operation.Syntax);
VerifyType(context.ReportDiagnostic, incrementOrDecrement.OperatorMethod.ContainingType, context.Operation.Syntax);
}

break;
case ITypeOfOperation typeOfOperation:
VerifyType(context.ReportDiagnostic, typeOfOperation.TypeOperand, context.Operation.Syntax);
Expand Down Expand Up @@ -220,6 +224,7 @@ bool VerifyTypeArguments(Action<Diagnostic> reportDiagnostic, ITypeSymbol? type,
return false;
}
}

break;

case IArrayTypeSymbol arrayTypeSymbol:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public bool Execute(GeneratorExecutionContext context)
RenderFormatMethod(memberIndent, language, strings, resourceString);
}
}

break;

case Lang.VisualBasic:
Expand All @@ -324,6 +325,7 @@ public bool Execute(GeneratorExecutionContext context)
{
throw new NotImplementedException();
}

break;

default:
Expand Down Expand Up @@ -378,6 +380,7 @@ public bool Execute(GeneratorExecutionContext context)
{memberIndent}}}
";
}

break;

case Lang.VisualBasic:
Expand All @@ -390,6 +393,7 @@ public bool Execute(GeneratorExecutionContext context)
{
throw new NotImplementedException();
}

break;

default:
Expand Down Expand Up @@ -450,6 +454,7 @@ public bool Execute(GeneratorExecutionContext context)
{resourceTypeDefinition}
}}";
}

break;

case Lang.VisualBasic:
Expand All @@ -461,6 +466,7 @@ public bool Execute(GeneratorExecutionContext context)
{resourceTypeDefinition}
End Namespace";
}

break;

default:
Expand Down Expand Up @@ -603,6 +609,7 @@ private static string CreateStringLiteral(string original, Lang lang)
{
stringLiteral.Append('@');
}

stringLiteral.Append('\"');
for (var i = 0; i < original.Length; i++)
{
Expand All @@ -611,8 +618,10 @@ private static string CreateStringLiteral(string original, Lang lang)
{
stringLiteral.Append('"');
}

stringLiteral.Append(original[i]);
}

stringLiteral.Append('\"');

return stringLiteral.ToString();
Expand Down Expand Up @@ -644,6 +653,7 @@ private static void RenderFormatMethod(string indent, Lang language, StringBuild
{
strings.AppendLine($@"{indent} => string.Format(Culture, GetResourceString(""{resourceString.Name}"") ?? """", {resourceString.GetArguments()});");
}

strings.AppendLine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
// We can't code fix if unsafe code isn't allowed.
return await document.GetSyntaxRootAsync(fixAllContext.CancellationToken);
}

var editor = await DocumentEditor.CreateAsync(document, fixAllContext.CancellationToken).ConfigureAwait(false);
SyntaxNode root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);

Expand All @@ -47,12 +48,14 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
{
identifierGenerator = scopeMap[block] = new IdentifierGenerator(editor.SemanticModel, block);
}

if (TryRewriteMethodCall(node, editor, identifierGenerator, addRenameAnnotation: false, fixAllContext.CancellationToken))
{
AddUnsafeModifierToEnclosingMethod(editor, node);
}
}
}

return editor.GetChangedRoot();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public IdentifierGenerator(SemanticModel model, IBlockOperation block)
{
identifier = NextIdentifier();
}

if (identifier is not null)
{
// The last identifier was not in use, so go back one to use it the next call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
// We can't code fix if unsafe code isn't allowed.
return;
}

SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

SyntaxNode enclosingNode = root.FindNode(context.Span);
Expand Down Expand Up @@ -64,6 +65,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return i;
}
}

return 0;
}

Expand Down Expand Up @@ -102,6 +104,7 @@ private static bool TryRewriteMethodCall(SyntaxNode node, DocumentEditor editor,
return true;
}
}

if (operation.TargetMethod.Name == "StructureToPtr" && operation.Arguments[0].Value.Type.IsUnmanagedType)
{
editor.ReplaceNode(syntax,
Expand All @@ -112,6 +115,7 @@ private static bool TryRewriteMethodCall(SyntaxNode node, DocumentEditor editor,
operation.Arguments[0].Value.Syntax));
return true;
}

if (operation.TargetMethod.Name == "PtrToStructure")
{
ITypeSymbol type;
Expand Down Expand Up @@ -178,6 +182,7 @@ private static bool TryRewriteMethodCall(SyntaxNode node, DocumentEditor editor,
{
return false;
}

editor.ReplaceNode(syntax, replacementNode);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected override async Task<Document> ImplementInterfacesOnDynamicCastableImpl
{
return null;
}

var methodDeclaration = generator.MethodDeclaration(method);
methodDeclaration = generator.WithModifiers(methodDeclaration, generator.GetModifiers(methodDeclaration).WithIsAbstract(false));
return generator.WithStatements(methodDeclaration, defaultMethodBodyStatements);
Expand All @@ -90,6 +91,7 @@ SyntaxNode GeneratePropertyImplementation(IPropertySymbol property)
{
propertyDeclaration = generator.WithGetAccessorStatements(propertyDeclaration, defaultMethodBodyStatements);
}

if (property.SetMethod is not null
&& model.Compilation.IsSymbolAccessibleWithin(property.SetMethod, type))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private sealed class CSharpDiagnosticHelper : DiagnosticHelper
{
return null;
}

var targetArg = attributeSyntax.ArgumentList.Arguments.FirstOrDefault(arg => arg.NameEquals.Name.IsEquivalentTo(targetNameSyntax, true));
return targetArg?.GetLocation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected override SyntaxNode GetHashDataSyntaxNode(PreferHashDataOverComputeHas
{
arg = arg.WithNameColon(arg.NameColon.WithName(SyntaxFactory.IdentifierName("source")));
}

var args = SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList(arg));
return SyntaxFactory.InvocationExpression(hashData, args);
}
Expand All @@ -69,6 +70,7 @@ protected override SyntaxNode GetHashDataSyntaxNode(PreferHashDataOverComputeHas
{
list[secondArgIndex] = secondArg.WithNameColon(SyntaxFactory.NameColon(SyntaxFactory.IdentifierName("start")));
}

var thirdArg = list[thirdArgIndex];
if (thirdArg.NameColon is not null)
{
Expand All @@ -90,6 +92,7 @@ protected override SyntaxNode GetHashDataSyntaxNode(PreferHashDataOverComputeHas
{
arg = arg.WithNameColon(SyntaxFactory.NameColon(SyntaxFactory.IdentifierName("source")));
}

var args = SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList(arg));
return SyntaxFactory.InvocationExpression(hashData, args);
}
Expand Down Expand Up @@ -135,6 +138,7 @@ protected override SyntaxNode FixHashCreateNode(SyntaxNode root, SyntaxNode crea
break;
}
}

return root;
}

Expand Down Expand Up @@ -165,6 +169,7 @@ private SyntaxNode MoveStatementsOutOfUsingStatementWithFormatting(SyntaxNode ro
newTrivia = AddRangeIfInteresting(newTrivia, usingStatement.GetTrailingTrivia());
statement = statement.WithTrailingTrivia(newTrivia);
}

return statement;
});

Expand Down Expand Up @@ -197,7 +202,6 @@ protected override bool IsInterestingTrivia(SyntaxTriviaList triviaList)
{
ns = qualifiedNamespaceSyntax.ToFullString();
}

}
else if (invocationNode.Expression is MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax { Expression: MemberAccessExpressionSyntax originalType } } })
{
Expand All @@ -214,7 +218,6 @@ protected override bool IsInterestingTrivia(SyntaxTriviaList triviaList)
ns = qualifiedNamespaceSyntax.ToFullString();
}


return ns;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ public sealed class CSharpPreferIsEmptyOverCountFixer : PreferIsEmptyOverCountFi
{
countNode = binaryExpression.Left;
}

break;
case UseCountProperlyAnalyzer.OperationBinaryRight:
if (node is BinaryExpressionSyntax binaryExpression2)
{
countNode = binaryExpression2.Right;
}

break;
case UseCountProperlyAnalyzer.OperationEqualsArgument:
if (node is InvocationExpressionSyntax invocationExpression)
{
countNode = invocationExpression.ArgumentList.Arguments[0].Expression;
}

break;
case UseCountProperlyAnalyzer.OperationEqualsInstance:
if (node is InvocationExpressionSyntax invocationExpression2)
Expand All @@ -47,6 +50,7 @@ public sealed class CSharpPreferIsEmptyOverCountFixer : PreferIsEmptyOverCountFi
countNode = memberAccess.Expression;
}
}

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected override bool TryGetArgumentName(SyntaxNode violatingNode, out string
argumentName = argumentSyntax.NameColon.Name.Identifier.ValueText;
return true;
}

return false;
}

Expand All @@ -38,6 +39,7 @@ protected override bool TryGetLiteralValueFromNode(SyntaxNode violatingNode, out
{
return TryGetCharFromLiteralExpressionSyntax(containedLiteralExpressionSyntax, out charLiteral);
}

return false;

static bool TryGetCharFromLiteralExpressionSyntax(LiteralExpressionSyntax sourceLiteralExpressionSyntax, out char parsedCharLiteral)
Expand All @@ -47,6 +49,7 @@ static bool TryGetCharFromLiteralExpressionSyntax(LiteralExpressionSyntax source
{
return true;
}

return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public sealed override void Initialize(AnalysisContext context)
{
continue;
}

goto case SyntaxKind.WhileStatement; // fall through

// Look for loops. We don't bother with ad-hoc loops via gotos as we're
Expand Down
Loading