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

Avoid Immutable.Add #4442

Merged
merged 3 commits into from
Dec 26, 2024
Merged
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
33 changes: 19 additions & 14 deletions src/Analyzers/MSTest.Analyzers/UseProperAssertMethodsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,14 @@ private static void AnalyzeIsTrueOrIsFalseInvocation(OperationAnalysisContext co

// The message is: Use 'Assert.{0}' instead of 'Assert.{1}'.
string properAssertMethod = shouldUseIsNull ? "IsNull" : "IsNotNull";

ImmutableDictionary<string, string?>.Builder properties = ImmutableDictionary.CreateBuilder<string, string?>();
properties.Add(ProperAssertMethodNameKey, properAssertMethod);
properties.Add(CodeFixModeKey, CodeFixModeSimple);
context.ReportDiagnostic(context.Operation.CreateDiagnostic(
Rule,
additionalLocations: ImmutableArray.Create(conditionArgument.Syntax.GetLocation(), expressionUnderTest.GetLocation()),
properties: ImmutableDictionary<string, string?>.Empty
.Add(ProperAssertMethodNameKey, properAssertMethod)
.Add(CodeFixModeKey, CodeFixModeSimple),
properties: properties.ToImmutable(),
properAssertMethod,
isTrueInvocation ? "IsTrue" : "IsFalse"));
return;
Expand All @@ -323,12 +325,13 @@ private static void AnalyzeIsTrueOrIsFalseInvocation(OperationAnalysisContext co

// The message is: Use 'Assert.{0}' instead of 'Assert.{1}'.
string properAssertMethod = shouldUseAreEqual ? "AreEqual" : "AreNotEqual";
ImmutableDictionary<string, string?>.Builder properties = ImmutableDictionary.CreateBuilder<string, string?>();
properties.Add(ProperAssertMethodNameKey, properAssertMethod);
properties.Add(CodeFixModeKey, CodeFixModeAddArgument);
context.ReportDiagnostic(context.Operation.CreateDiagnostic(
Rule,
additionalLocations: ImmutableArray.Create(conditionArgument.Syntax.GetLocation(), toBecomeExpected.GetLocation(), toBecomeActual.GetLocation()),
properties: ImmutableDictionary<string, string?>.Empty
.Add(ProperAssertMethodNameKey, properAssertMethod)
.Add(CodeFixModeKey, CodeFixModeAddArgument),
properties: properties.ToImmutable(),
properAssertMethod,
isTrueInvocation ? "IsTrue" : "IsFalse"));
return;
Expand All @@ -354,19 +357,19 @@ actualArgumentValue.Type is { } actualType &&
actualType.SpecialType != SpecialType.System_Boolean &&
!actualType.IsNullableOfBoolean();

ImmutableDictionary<string, string?> properties = ImmutableDictionary<string, string?>.Empty
.Add(ProperAssertMethodNameKey, properAssertMethod)
.Add(CodeFixModeKey, CodeFixModeRemoveArgument);
ImmutableDictionary<string, string?>.Builder properties = ImmutableDictionary.CreateBuilder<string, string?>();
properties.Add(ProperAssertMethodNameKey, properAssertMethod);
properties.Add(CodeFixModeKey, CodeFixModeRemoveArgument);

if (codeFixShouldAddCast)
{
properties = properties.Add(NeedsNullableBooleanCastKey, null);
properties.Add(NeedsNullableBooleanCastKey, null);
}

context.ReportDiagnostic(context.Operation.CreateDiagnostic(
Rule,
additionalLocations: ImmutableArray.Create(expectedArgument.Syntax.GetLocation(), actualArgumentValue?.Syntax.GetLocation() ?? Location.None),
properties: properties,
properties: properties.ToImmutable(),
properAssertMethod,
isAreEqualInvocation ? "AreEqual" : "AreNotEqual"));
}
Expand All @@ -379,12 +382,14 @@ actualArgumentValue.Type is { } actualType &&

// The message is: Use 'Assert.{0}' instead of 'Assert.{1}'.
string properAssertMethod = shouldUseIsNull ? "IsNull" : "IsNotNull";
ImmutableDictionary<string, string?>.Builder properties = ImmutableDictionary.CreateBuilder<string, string?>();
properties.Add(ProperAssertMethodNameKey, properAssertMethod);
properties.Add(CodeFixModeKey, CodeFixModeRemoveArgument);
context.ReportDiagnostic(context.Operation.CreateDiagnostic(
Rule,
additionalLocations: ImmutableArray.Create(expectedArgument.Syntax.GetLocation()),
properties: ImmutableDictionary<string, string?>.Empty
.Add(ProperAssertMethodNameKey, properAssertMethod)
.Add(CodeFixModeKey, CodeFixModeRemoveArgument), properAssertMethod,
properties: properties.ToImmutable(),
properAssertMethod,
isAreEqualInvocation ? "AreEqual" : "AreNotEqual"));
}
}
Expand Down
Loading