Skip to content

Commit 4fdaf14

Browse files
fix: wrap with ParenthesizedExpression when casting the subject (#183)
* add test to reproduce #172 * wrap with ParenthesizedExpression when casting the subject --------- Co-authored-by: Meir Blachman <meblachm@microsoft.com>
1 parent 385ac6f commit 4fdaf14

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,49 @@ public static void Main()
258258

259259
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
260260
}
261+
262+
[TestMethod]
263+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/172")]
264+
public void AssertAreEqualDoesNotCompile()
265+
{
266+
const string oldSource = @"
267+
using FluentAssertions;
268+
using FluentAssertions.Extensions;
269+
270+
namespace TestProject
271+
{
272+
using Microsoft.VisualStudio.TestTools.UnitTesting;
273+
274+
public class Program
275+
{
276+
public static void Main()
277+
{
278+
double x = 5;
279+
280+
Assert.AreEqual(1, (int)x);
281+
}
282+
}
283+
}";
284+
const string newSource = @"
285+
using FluentAssertions;
286+
using FluentAssertions.Extensions;
287+
288+
namespace TestProject
289+
{
290+
using Microsoft.VisualStudio.TestTools.UnitTesting;
291+
292+
public class Program
293+
{
294+
public static void Main()
295+
{
296+
double x = 5;
297+
298+
((int)x).Should().Be(1);
299+
}
300+
}
301+
}";
302+
303+
DiagnosticVerifier.VerifyCSharpFix<AssertAreEqualCodeFix, AssertAreEqualAnalyzer>(oldSource, newSource);
304+
}
261305
}
262306
}

src/FluentAssertions.Analyzers/Utilities/Expressions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public static ArgumentSyntax OptionsUsing(ArgumentSyntax comparer)
2626

2727
public static InvocationExpressionSyntax SubjectShould(ExpressionSyntax subject)
2828
{
29+
if (subject.IsKind(SyntaxKind.CastExpression))
30+
{
31+
return SF.InvocationExpression(
32+
SF.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SF.ParenthesizedExpression(subject), SF.IdentifierName("Should")),
33+
SF.ArgumentList()
34+
);
35+
}
36+
2937
return SF.InvocationExpression(
3038
SF.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, subject, SF.IdentifierName("Should")),
3139
SF.ArgumentList()

0 commit comments

Comments
 (0)