Skip to content

Commit

Permalink
Update to .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Nov 15, 2023
1 parent c336ad4 commit c8cd592
Show file tree
Hide file tree
Showing 64 changed files with 219 additions and 505 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@ dotnet_diagnostic.MA0033.severity = warning
dotnet_diagnostic.MA0035.severity = warning
dotnet_diagnostic.MA0036.severity = suggestion
dotnet_diagnostic.MA0037.severity = error
dotnet_diagnostic.MA0038.severity = suggestion
dotnet_diagnostic.MA0038.severity = none
dotnet_diagnostic.MA0039.severity = error
dotnet_diagnostic.MA0040.severity = suggestion
dotnet_diagnostic.MA0041.severity = suggestion
dotnet_diagnostic.MA0041.severity = none
dotnet_diagnostic.MA0042.severity = suggestion
dotnet_diagnostic.MA0043.severity = suggestion
dotnet_diagnostic.MA0044.severity = suggestion
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Polyfill" Version="1.0.26">
<PackageReference Include="Meziantou.Polyfill" Version="1.0.30">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
<Otherwise>
<ItemGroup>
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0-beta1.23472.1" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.8.0-2.final" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0-2.final" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0-2.final" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.8.0-3.final" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0-3.final" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0-3.final" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);ROSLYN4_4;ROSLYN_4_2_OR_GREATER;ROSLYN_4_4_OR_GREATER;ROSLYN_4_5_OR_GREATER;ROSLYN_4_6_OR_GREATER;ROSLYN_4_8_OR_GREATER</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.400",
"version": "8.0.100",
"rollForward": "latestMajor"
}
}
2 changes: 1 addition & 1 deletion src/DocumentationGenerator/DocumentationGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/ListDotNetTypes/ListDotNetTypes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<ImplicitUsings>true</ImplicitUsings>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<IncludeBuildOutput>false</IncludeBuildOutput>
<Version>1.0.1</Version>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ private static async Task<Document> ReplaceWithMethodName(Document document, Syn
var generator = editor.Generator;

var invocation = (InvocationExpressionSyntax)nodeToFix;
var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if (memberAccess == null)
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
return document;

var newNode = nodeToFix.ReplaceNode(memberAccess.Name, generator.IdentifierName(methodName));
Expand Down
24 changes: 6 additions & 18 deletions src/Meziantou.Analyzer.CodeFixers/Rules/OptimizeLinqUsageFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ private static async Task<Document> UseCastInsteadOfSelect(Document document, Sy

var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var generator = editor.Generator;
var operation = editor.SemanticModel.GetOperation(selectInvocationExpression, cancellationToken) as IInvocationOperation;
if (operation == null)
if (editor.SemanticModel.GetOperation(selectInvocationExpression, cancellationToken) is not IInvocationOperation operation)
return document;

var type = operation.TargetMethod.TypeArguments[1];
Expand Down Expand Up @@ -647,7 +646,7 @@ private static SyntaxNode ReplaceParameter(IAnonymousFunctionOperation method, s
return invocationExpression.Expression as MemberAccessExpressionSyntax;
}

private static SyntaxNode? GetParentMemberExpression(SyntaxNode invocationExpressionSyntax)
private static ExpressionSyntax? GetParentMemberExpression(SyntaxNode invocationExpressionSyntax)
{
var memberAccessExpression = GetMemberAccessExpression(invocationExpressionSyntax);
if (memberAccessExpression == null)
Expand All @@ -656,25 +655,14 @@ private static SyntaxNode ReplaceParameter(IAnonymousFunctionOperation method, s
return memberAccessExpression.Expression;
}

private sealed class ParameterRewriter : CSharpSyntaxRewriter
private sealed class ParameterRewriter(SemanticModel semanticModel, IParameterSymbol parameterSymbol, string newParameterName) : CSharpSyntaxRewriter
{
private readonly SemanticModel _semanticModel;
private readonly IParameterSymbol _parameterSymbol;
private readonly string _newParameterName;

public ParameterRewriter(SemanticModel semanticModel, IParameterSymbol parameterSymbol, string newParameterName)
{
_semanticModel = semanticModel;
_parameterSymbol = parameterSymbol;
_newParameterName = newParameterName;
}

public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
{
var symbol = _semanticModel.GetSymbolInfo(node).Symbol;
if (symbol != null && symbol.IsEqualTo(_parameterSymbol))
var symbol = semanticModel.GetSymbolInfo(node).Symbol;
if (symbol != null && symbol.IsEqualTo(parameterSymbol))
{
return IdentifierName(_newParameterName);
return IdentifierName(newParameterName);
}

return base.VisitIdentifierName(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> UseIs(Document document, SyntaxNode nodeToFix, CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var operation = editor.SemanticModel.GetOperation(nodeToFix, cancellationToken) as IInvocationOperation;
if (operation == null)
if (editor.SemanticModel.GetOperation(nodeToFix, cancellationToken) is not IInvocationOperation operation)
return document;

var newExpression = SyntaxFactory.IsPatternExpression((ExpressionSyntax)operation.Arguments[0].Value.Syntax, SyntaxFactory.ConstantPattern((ExpressionSyntax)operation.Arguments[1].Value.Syntax));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ private static async Task<Document> ConvertToSourceGenerator(Document document,
// Extract arguments (pattern,options,timeout)
var attributes = generator.Attribute(generator.TypeExpression(regexGeneratorAttributeSymbol), attributeArguments: (patternValue, regexOptionsValue, timeoutValue) switch
{
({ }, null, null) => new[] { patternValue },
({ }, { }, null) => new[] { patternValue, regexOptionsValue },
({ }, { }, { }) => new[] { patternValue, regexOptionsValue, AttributeArgument((ExpressionSyntax)timeoutValue).WithNameColon(NameColon(IdentifierName("matchTimeoutMilliseconds"))) },
({ }, null, null) => [patternValue],
({ }, { }, null) => [patternValue, regexOptionsValue],
({ }, { }, { }) => [patternValue, regexOptionsValue, AttributeArgument((ExpressionSyntax)timeoutValue).WithNameColon(NameColon(IdentifierName("matchTimeoutMilliseconds")))],
_ => Array.Empty<SyntaxNode>(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Differencing;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
Expand Down
5 changes: 2 additions & 3 deletions src/Meziantou.Analyzer/Internals/AwaitableTypes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Operations;
Expand Down Expand Up @@ -28,7 +27,7 @@ public AwaitableTypes(Compilation compilation)
}
else
{
_taskOrValueTaskSymbols = Array.Empty<INamedTypeSymbol>();
_taskOrValueTaskSymbols = [];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
using System.Linq;
using System.Security.Cryptography;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Operations;

namespace Meziantou.Analyzer.Internals;

internal sealed class CultureSensitiveFormattingContext
internal sealed class CultureSensitiveFormattingContext(Compilation compilation)
{
public CultureSensitiveFormattingContext(Compilation compilation)
{
FormatProviderSymbol = compilation.GetBestTypeByMetadataName("System.IFormatProvider");
CultureInfoSymbol = compilation.GetBestTypeByMetadataName("System.Globalization.CultureInfo");
NumberStyleSymbol = compilation.GetBestTypeByMetadataName("System.Globalization.NumberStyles");
DateTimeStyleSymbol = compilation.GetBestTypeByMetadataName("System.Globalization.DateTimeStyles");
StringBuilderSymbol = compilation.GetBestTypeByMetadataName("System.Text.StringBuilder");
StringBuilder_AppendInterpolatedStringHandlerSymbol = compilation.GetBestTypeByMetadataName("System.Text.StringBuilder+AppendInterpolatedStringHandler");
GuidSymbol = compilation.GetBestTypeByMetadataName("System.Guid");
EnumSymbol = compilation.GetBestTypeByMetadataName("System.Enum");
DateTimeOffsetSymbol = compilation.GetBestTypeByMetadataName("System.DateTimeOffset");
DateOnlySymbol = compilation.GetBestTypeByMetadataName("System.DateOnly");
TimeOnlySymbol = compilation.GetBestTypeByMetadataName("System.TimeOnly");
UInt128Symbol = compilation.GetBestTypeByMetadataName("System.UInt128");
UriSymbol = compilation.GetBestTypeByMetadataName("System.Uri");
TimeSpanSymbol = compilation.GetBestTypeByMetadataName("System.TimeSpan");
VersionSymbol = compilation.GetBestTypeByMetadataName("System.Version");
SystemIFormattableSymbol = compilation.GetBestTypeByMetadataName("System.IFormattable");
SystemWindowsFontStretchSymbol = compilation.GetBestTypeByMetadataName("System.Windows.FontStretch");
SystemWindowsMediaBrushSymbol = compilation.GetBestTypeByMetadataName("System.Windows.Media.Brush");
}

public INamedTypeSymbol? FormatProviderSymbol { get; }
public INamedTypeSymbol? CultureInfoSymbol { get; }
public INamedTypeSymbol? NumberStyleSymbol { get; }
public INamedTypeSymbol? DateTimeStyleSymbol { get; }
public INamedTypeSymbol? StringBuilderSymbol { get; }
public INamedTypeSymbol? StringBuilder_AppendInterpolatedStringHandlerSymbol { get; }
public INamedTypeSymbol? GuidSymbol { get; }
public INamedTypeSymbol? EnumSymbol { get; }
public INamedTypeSymbol? DateTimeOffsetSymbol { get; }
public INamedTypeSymbol? DateOnlySymbol { get; }
public INamedTypeSymbol? TimeOnlySymbol { get; }
public INamedTypeSymbol? UInt128Symbol { get; }
public INamedTypeSymbol? UriSymbol { get; }
public INamedTypeSymbol? TimeSpanSymbol { get; }
public INamedTypeSymbol? VersionSymbol { get; }
public INamedTypeSymbol? SystemIFormattableSymbol { get; }
public INamedTypeSymbol? SystemWindowsFontStretchSymbol { get; }
public INamedTypeSymbol? SystemWindowsMediaBrushSymbol { get; }
public INamedTypeSymbol? FormatProviderSymbol { get; } = compilation.GetBestTypeByMetadataName("System.IFormatProvider");
public INamedTypeSymbol? CultureInfoSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Globalization.CultureInfo");
public INamedTypeSymbol? NumberStyleSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Globalization.NumberStyles");
public INamedTypeSymbol? DateTimeStyleSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Globalization.DateTimeStyles");
public INamedTypeSymbol? StringBuilderSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Text.StringBuilder");
public INamedTypeSymbol? StringBuilder_AppendInterpolatedStringHandlerSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Text.StringBuilder+AppendInterpolatedStringHandler");
public INamedTypeSymbol? GuidSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Guid");
public INamedTypeSymbol? EnumSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Enum");
public INamedTypeSymbol? DateTimeOffsetSymbol { get; } = compilation.GetBestTypeByMetadataName("System.DateTimeOffset");
public INamedTypeSymbol? DateOnlySymbol { get; } = compilation.GetBestTypeByMetadataName("System.DateOnly");
public INamedTypeSymbol? TimeOnlySymbol { get; } = compilation.GetBestTypeByMetadataName("System.TimeOnly");
public INamedTypeSymbol? UInt128Symbol { get; } = compilation.GetBestTypeByMetadataName("System.UInt128");
public INamedTypeSymbol? UriSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Uri");
public INamedTypeSymbol? TimeSpanSymbol { get; } = compilation.GetBestTypeByMetadataName("System.TimeSpan");
public INamedTypeSymbol? VersionSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Version");
public INamedTypeSymbol? SystemIFormattableSymbol { get; } = compilation.GetBestTypeByMetadataName("System.IFormattable");
public INamedTypeSymbol? SystemWindowsFontStretchSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Windows.FontStretch");
public INamedTypeSymbol? SystemWindowsMediaBrushSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Windows.Media.Brush");

private static bool MustUnwrapNullableOfT(CultureSensitiveOptions options)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Meziantou.Analyzer/Internals/MethodSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Meziantou.Analyzer;

internal static class MethodSymbolExtensions
{
private static readonly string[] s_msTestNamespaceParts = new[] { "Microsoft", "VisualStudio", "TestTools", "UnitTesting" };
private static readonly string[] s_nunitNamespaceParts = new[] { "NUnit", "Framework" };
private static readonly string[] s_xunitNamespaceParts = new[] { "Xunit" };
private static readonly string[] s_msTestNamespaceParts = ["Microsoft", "VisualStudio", "TestTools", "UnitTesting"];
private static readonly string[] s_nunitNamespaceParts = ["NUnit", "Framework"];
private static readonly string[] s_xunitNamespaceParts = ["Xunit"];

public static bool IsInterfaceImplementation(this IMethodSymbol method)
{
Expand Down
9 changes: 2 additions & 7 deletions src/Meziantou.Analyzer/Internals/OperationUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace Meziantou.Analyzer;

internal sealed class OperationUtilities
internal sealed class OperationUtilities(Compilation compilation)
{
private readonly INamedTypeSymbol? _expressionSymbol;

public OperationUtilities(Compilation compilation)
{
_expressionSymbol = compilation.GetBestTypeByMetadataName("System.Linq.Expressions.Expression");
}
private readonly INamedTypeSymbol? _expressionSymbol = compilation.GetBestTypeByMetadataName("System.Linq.Expressions.Expression");

public bool IsInExpressionContext(IOperation operation)
{
Expand Down
13 changes: 3 additions & 10 deletions src/Meziantou.Analyzer/Internals/OverloadFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
using Microsoft.CodeAnalysis;

namespace Meziantou.Analyzer.Internals;
internal sealed class OverloadFinder
internal sealed class OverloadFinder(Compilation compilation)
{
private readonly ITypeSymbol? _obsoleteSymbol;
private readonly Compilation _compilation;

public OverloadFinder(Compilation compilation)
{
_compilation = compilation;
_obsoleteSymbol = compilation.GetBestTypeByMetadataName("System.ObsoleteAttribute");
}
private readonly ITypeSymbol? _obsoleteSymbol = compilation.GetBestTypeByMetadataName("System.ObsoleteAttribute");

public bool HasOverloadWithAdditionalParameterOfType(
IMethodSymbol methodSymbol,
Expand Down Expand Up @@ -75,7 +68,7 @@ public bool HasOverloadWithAdditionalParameterOfType(
ImmutableArray<ISymbol> members;
if (syntaxNode != null)
{
var semanticModel = _compilation.GetSemanticModel(syntaxNode.SyntaxTree);
var semanticModel = compilation.GetSemanticModel(syntaxNode.SyntaxTree);
members = semanticModel.LookupSymbols(syntaxNode.GetLocation().SourceSpan.End, methodSymbol.ContainingType, methodSymbol.Name, includeReducedExtensionMethods: true);
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/Meziantou.Analyzer/Internals/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public bool MoveNext()
var index = span.IndexOfAny('\r', '\n');
if (index == -1)
{
_str = ReadOnlySpan<char>.Empty;
Current = new LineSplitEntry(span, ReadOnlySpan<char>.Empty);
_str = [];
Current = new LineSplitEntry(span, []);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Meziantou.Analyzer/Internals/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Meziantou.Analyzer;
// http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Shared/Extensions/ITypeSymbolExtensions.cs,190b4ed0932458fd,references
internal static class TypeSymbolExtensions
{
private static readonly string[] Microsoft_VisualStudio_TestTools_UnitTesting = { "Microsoft", "VisualStudio", "TestTools", "UnitTesting" };
private static readonly string[] NUnit_Framework = { "NUnit", "Framework" };
private static readonly string[] Xunit = { "Xunit" };
private static readonly string[] Microsoft_VisualStudio_TestTools_UnitTesting = ["Microsoft", "VisualStudio", "TestTools", "UnitTesting"];
private static readonly string[] NUnit_Framework = ["NUnit", "Framework"];
private static readonly string[] Xunit = ["Xunit"];

public static IList<INamedTypeSymbol> GetAllInterfacesIncludingThis(this ITypeSymbol type)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Meziantou.Analyzer/Meziantou.Analyzer.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<IncludeBuildOutput>false</IncludeBuildOutput>
<Version>1.0.1</Version>

Expand Down Expand Up @@ -31,7 +31,7 @@
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>

<PackageReference Include="Meziantou.Analyzer" Version="2.0.83" Condition="'$(PackageId)' != 'Meziantou.Analyzer'">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.109" Condition="'$(PackageId)' != 'Meziantou.Analyzer'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace Meziantou.Analyzer.Rules;
namespace Meziantou.Analyzer.Rules;

internal static class ArgumentExceptionShouldSpecifyArgumentNameAnalyzerCommon
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Meziantou.Analyzer.Rules;

Expand Down
Loading

0 comments on commit c8cd592

Please sign in to comment.