Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ private static async Task<Document> UseCountGreaterThanZero(Document document, S

var generator = editor.Generator;
var countExpression = generator.MemberAccessExpression(invocation.Arguments[0].Syntax, "Count");
var newExpression = generator.ValueNotEqualsExpression(countExpression, generator.LiteralExpression(0));

// The invocation may be the operand of an operator or the target of a member access, both of which bind
// tighter than '!=', so the comparison must be parenthesized. Simplifier removes the useless parentheses.
var newExpression = generator.ValueNotEqualsExpression(countExpression, generator.LiteralExpression(0))
.Parenthesize()
.WithTrailingTrivia(nodeToFix.GetTrailingTrivia());

editor.ReplaceNode(nodeToFix, newExpression);
return editor.GetChangedDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ private static async Task<Document> UseIs(Document document, SyntaxNode nodeToFi
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var operation = (IInvocationOperation)editor.SemanticModel.GetOperation(nodeToFix, cancellationToken)!;

var newExpression = SyntaxFactory.IsPatternExpression((ExpressionSyntax)operation.Arguments[0].Value.Syntax, SyntaxFactory.ConstantPattern((ExpressionSyntax)operation.Arguments[1].Value.Syntax));
// The invocation may be the operand of an operator or the target of a member access, both of which bind
// tighter than 'is', so the pattern must be parenthesized. Simplifier removes the useless parentheses.
var newExpression = SyntaxFactory.IsPatternExpression((ExpressionSyntax)operation.Arguments[0].Value.Syntax, SyntaxFactory.ConstantPattern((ExpressionSyntax)operation.Arguments[1].Value.Syntax))
.Parenthesize()
.WithTrailingTrivia(nodeToFix.GetTrailingTrivia());

editor.ReplaceNode(nodeToFix, newExpression);
return editor.GetChangedDocument();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,102 @@ public Test()
return test.RunAsync();
}

[Fact]
public Task Any_List_Negated_CodeFix()
{
var test = new CodeFixTest();
test.TestCode = """
using System.Linq;
class Test
{
public Test()
{
var collection = new System.Collections.Generic.List<int>();
if (!{|MA0112:collection.Any()|}) { }
}
}

""";
test.FixedCode = """
using System.Linq;
class Test
{
public Test()
{
var collection = new System.Collections.Generic.List<int>();
if (!(collection.Count != 0)) { }
}
}

""";

return test.RunAsync();
}

[Fact]
public Task Any_List_MemberAccess_CodeFix()
{
var test = new CodeFixTest();
test.TestCode = """
using System.Linq;
class Test
{
public Test()
{
var collection = new System.Collections.Generic.List<int>();
_ = {|MA0112:collection.Any()|}.ToString();
}
}

""";
test.FixedCode = """
using System.Linq;
class Test
{
public Test()
{
var collection = new System.Collections.Generic.List<int>();
_ = (collection.Count != 0).ToString();
}
}

""";

return test.RunAsync();
}

[Fact]
public Task Any_List_KeepsTrivia_CodeFix()
{
var test = new CodeFixTest();
test.TestCode = """
using System.Linq;
class Test
{
public Test()
{
var collection = new System.Collections.Generic.List<int>();
_ = /* before */ {|MA0112:collection.Any()|} /* after */;
}
}

""";
test.FixedCode = """
using System.Linq;
class Test
{
public Test()
{
var collection = new System.Collections.Generic.List<int>();
_ = /* before */ collection.Count != 0 /* after */;
}
}

""";

return test.RunAsync();
}

[Fact]
public Task Any_Array()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,60 @@ public Task ReadOnlySpanChar_EqualsOrdinal()
return test.RunAsync();
}

[Fact]
public Task ReadOnlySpanChar_SequenceEqual_Negated()
{
var test = CreateTest();
test.TestCode = """
using System;
Span<char> str = default;
if (!{|MA0128:str.SequenceEqual("bar")|}) { }
""";
test.FixedCode = """
using System;
Span<char> str = default;
if (!(str is "bar")) { }
""";

return test.RunAsync();
}

[Fact]
public Task ReadOnlySpanChar_SequenceEqual_MemberAccess()
{
var test = CreateTest();
test.TestCode = """
using System;
Span<char> str = default;
_ = {|MA0128:str.SequenceEqual("bar")|}.ToString();
""";
test.FixedCode = """
using System;
Span<char> str = default;
_ = (str is "bar").ToString();
""";

return test.RunAsync();
}

[Fact]
public Task ReadOnlySpanChar_SequenceEqual_KeepsTrivia()
{
var test = CreateTest();
test.TestCode = """
using System;
Span<char> str = default;
_ = /* before */ {|MA0128:str.SequenceEqual("bar")|} /* after */;
""";
test.FixedCode = """
using System;
Span<char> str = default;
_ = /* before */ str is "bar" /* after */;
""";

return test.RunAsync();
}

[Fact]
public Task ReadOnlySpanChar_EqualsOrdinalIgnoreCase()
{
Expand Down