Skip to content

Commit

Permalink
Fix formatting, apply various code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Apr 15, 2023
1 parent 19fa5fa commit faf227d
Show file tree
Hide file tree
Showing 52 changed files with 124 additions and 176 deletions.
7 changes: 5 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ root = true

# Code files
[*.{cs,csx}]

dotnet_sort_system_directives_first = true

csharp_style_namespace_declarations = file_scoped:suggestion

roslynator_accessibility_modifiers = explicit
roslynator_accessor_braces_style = single_line_when_expression_is_on_single_line
roslynator_array_creation_type_style = implicit_when_type_is_obvious
Expand Down Expand Up @@ -38,8 +43,6 @@ roslynator_enum_flag_value_style = shift_operator
roslynator_blank_line_after_file_scoped_namespace_declaration = true
roslynator_null_check_style = pattern_matching

csharp_style_namespace_declarations = file_scoped:suggestion

dotnet_diagnostic.RCS0001.severity = suggestion
dotnet_diagnostic.RCS0003.severity = suggestion
dotnet_diagnostic.RCS0004.severity = suggestion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
using Roslynator.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Roslynator.CSharp.CSharpFactory;
using static Roslynator.CSharp.SyntaxRefactorings;
using static Roslynator.CSharp.SyntaxInfo;
using static Roslynator.CSharp.SyntaxRefactorings;

namespace Roslynator.CSharp.CodeFixes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CodeFixes;
using Roslynator.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;

namespace Roslynator.CSharp.CodeFixes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Roslynator.CodeFixes;
using Roslynator.CSharp.Refactorings.Documentation;
using Microsoft.CodeAnalysis.CSharp;

namespace Roslynator.CSharp.CodeFixes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;

namespace Roslynator.CSharp.CodeFixes;

Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/Analysis/WhitespaceAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.CSharp;

namespace Roslynator.CSharp.Analysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using System.Diagnostics.CodeAnalysis;

namespace Roslynator.CSharp.Analysis;

Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/CSharp/Analysis/DefaultExpressionAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Threading;

namespace Roslynator.CSharp.Analysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public static void AnalyzerNamedTypeSymbol(SymbolAnalysisContext context, INamed

if (typeSymbol.OriginalDefinition.HasAttribute(debuggerDisplayAttributeSymbol, includeBaseTypes: true))
return;
if(typeSymbol.GetSyntax(context.CancellationToken) is not TypeDeclarationSyntax typeDeclaration)

if (typeSymbol.GetSyntax(context.CancellationToken) is not TypeDeclarationSyntax typeDeclaration)
return;
var identifier = typeDeclaration.Identifier;

SyntaxToken identifier = typeDeclaration.Identifier;

DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.MarkTypeWithDebuggerDisplayAttribute, identifier, identifier.ValueText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public static void OptimizeAdd(SyntaxNodeAnalysisContext context, in SimpleMembe
&& (block is null
|| SyntaxTriviaAnalysis.IsExteriorTriviaEmptyOrWhitespace(block.OpenBraceToken)))
{
var forEachVariableSymbol = semanticModel.GetDeclaredSymbol(forEachStatement, cancellationToken) as ILocalSymbol;
ILocalSymbol forEachVariableSymbol = semanticModel.GetDeclaredSymbol(forEachStatement, cancellationToken);

if (forEachVariableSymbol is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static void AnalyzeBaseArgumentList(SyntaxNodeAnalysisContext context)
if (firstIndex >= 0
&& firstIndex < arguments.Count - 1)
{
ISymbol symbol = semanticModel.GetSymbol(arguments.First().Parent.Parent, cancellationToken);
ISymbol symbol = semanticModel.GetSymbol(arguments[0].Parent.Parent, cancellationToken);

if (symbol is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static void AnalyzeParenthesizedExpression(SyntaxNodeAnalysisContext con
case SyntaxKind.EqualsExpression:
case SyntaxKind.NotEqualsExpression:
case SyntaxKind.SimpleMemberAccessExpression:
{
{
if (expression.IsKind(SyntaxKind.IdentifierName)
|| expression is LiteralExpressionSyntax)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ static bool AnalyzeTrivia(SyntaxTriviaList trivia)

private static bool LocallyDeclaredVariablesOverlapWithAnyOtherSwitchSections(SwitchStatementSyntax switchStatement, BlockSyntax switchBlock, SemanticModel semanticModel)
{
var sectionVariablesDeclared = semanticModel.AnalyzeDataFlow(switchBlock)!
ImmutableArray<ISymbol> sectionVariablesDeclared = semanticModel.AnalyzeDataFlow(switchBlock)!
.VariablesDeclared;

if (sectionVariablesDeclared.IsEmpty)
return false;

var sectionDeclaredVariablesNames = sectionVariablesDeclared
ImmutableHashSet<string> sectionDeclaredVariablesNames = sectionVariablesDeclared
.Select(s => s.Name)
.ToImmutableHashSet();

foreach (var otherSection in switchStatement.Sections)
foreach (SwitchSectionSyntax otherSection in switchStatement.Sections)
{
// If the other section is not a block then we do not need to check as if there were overlapping variables then there would already be a error.
if (otherSection.Statements.SingleOrDefault(shouldThrow: false) is not BlockSyntax otherBlock)
Expand All @@ -128,14 +128,13 @@ private static bool LocallyDeclaredVariablesOverlapWithAnyOtherSwitchSections(Sw
if (otherBlock.Span == switchBlock.Span)
continue;

foreach (var v in semanticModel.AnalyzeDataFlow(otherBlock)!.VariablesDeclared)
foreach (ISymbol v in semanticModel.AnalyzeDataFlow(otherBlock)!.VariablesDeclared)
{
if (sectionDeclaredVariablesNames.Contains(v.Name))
return true;
}
}

return false;
}


}
21 changes: 10 additions & 11 deletions src/Analyzers/CSharp/Analysis/RemoveUnnecessaryElseAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -61,35 +60,35 @@ private static bool IsFixable(ElseClauseSyntax elseClause, SemanticModel semanti

if (ifStatementStatement is not BlockSyntax ifBlock)
return CSharpFacts.IsJumpStatement(ifStatementStatement.Kind());

if (elseClause.Statement is BlockSyntax elseBlock && LocalDeclaredVariablesOverlap(elseBlock, ifBlock, semanticModel))
return false;

var lastStatementInIf = ifBlock.Statements.LastOrDefault();
StatementSyntax lastStatementInIf = ifBlock.Statements.LastOrDefault();

return lastStatementInIf is not null
&& CSharpFacts.IsJumpStatement(lastStatementInIf.Kind());
&& CSharpFacts.IsJumpStatement(lastStatementInIf.Kind());
}

private static bool LocalDeclaredVariablesOverlap(BlockSyntax elseBlock, BlockSyntax ifBlock, SemanticModel semanticModel)
{
var elseVariablesDeclared = semanticModel.AnalyzeDataFlow(elseBlock)!
{
ImmutableArray<ISymbol> elseVariablesDeclared = semanticModel.AnalyzeDataFlow(elseBlock)!
.VariablesDeclared;

if (elseVariablesDeclared.IsEmpty)
return false;

var ifVariablesDeclared = semanticModel.AnalyzeDataFlow(ifBlock)!
ImmutableArray<ISymbol> ifVariablesDeclared = semanticModel.AnalyzeDataFlow(ifBlock)!
.VariablesDeclared;

if (ifVariablesDeclared.IsEmpty)
return false;

var elseVariableNames = elseVariablesDeclared
ImmutableHashSet<string> elseVariableNames = elseVariablesDeclared
.Select(s => s.Name)
.ToImmutableHashSet();

foreach (var v in ifVariablesDeclared)
foreach (ISymbol v in ifVariablesDeclared)
{
if (elseVariableNames.Contains(v.Name))
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;

namespace Roslynator.CSharp.Analysis;

Expand Down
2 changes: 1 addition & 1 deletion src/CSharp/CSharp/IfStatementCascadeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Roslynator.CSharp;

Expand Down
2 changes: 1 addition & 1 deletion src/CSharp/CSharp/StringLiteralTextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.Text;
using Roslynator.CSharp.Syntax;
using Roslynator.Text;

namespace Roslynator.CSharp;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Generic;

namespace Roslynator.CodeAnalysis.CSharp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
Expand Down Expand Up @@ -32,13 +31,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

if (!TryFindFirstAncestorOrSelf(root, context.Span, out LabeledStatementSyntax labeledStatement))
return;
var codeAction = CodeAction.Create(

CodeAction codeAction = CodeAction.Create(
"Remove unused label",
ct => context.Document.ReplaceNodeAsync(labeledStatement, labeledStatement.Statement, ct),
EquivalenceKey.Create(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);

context.RegisterCodeFix(codeAction, diagnostic);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis;
using Roslynator.Configuration;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.Configuration;

namespace Roslynator.CodeFixes;

Expand Down
29 changes: 0 additions & 29 deletions src/CommandLine/Commands/MigrateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,35 +178,6 @@ private CommandStatus ExecuteFile(string path)
return CommandStatus.NotSuccess;
}

private CommandStatus ExecuteProject(string path)
{
XDocument document;
try
{
document = XDocument.Load(path, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
}
catch (XmlException ex)
{
WriteLine($"Cannot load '{path}'", Colors.Message_Warning, Verbosity.Minimal);
WriteError(ex, verbosity: Verbosity.Minimal);
return CommandStatus.NotSuccess;
}

XElement root = document.Root;

if (root.Attribute("Sdk")?.Value == "Microsoft.NET.Sdk")
{
WriteLine($"Analyze '{path}'", Verbosity.Detailed);
return ExecuteProject(path, document);
}
else
{
WriteLine($"Project does not support migration: '{path}'", Colors.Message_Warning, Verbosity.Detailed);

return CommandStatus.NotSuccess;
}
}

private CommandStatus ExecuteProject(string path, XDocument document)
{
List<LogMessage> messages = null;
Expand Down
6 changes: 3 additions & 3 deletions src/CommandLine/Orang/CommandLine.Core/CommandLoader.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// This code is originally from https://github.com/josefpihrt/orang. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using CommandLine;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

namespace Roslynator;

Expand Down
Loading

0 comments on commit faf227d

Please sign in to comment.