Skip to content

Conversation

@Evangelink
Copy link
Member

Fixes #7204

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR ensures that MSTest analyzer code fixers correctly handle indentation and newlines when applying fixes. The changes address issue #7204 by improving trivia preservation in several code fixers and adding comprehensive test coverage for multi-line scenarios.

Changes:

  • Added trivia preservation logic to multiple code fixers to maintain original indentation and whitespace
  • Added 20+ new test cases covering multi-line scenarios across various analyzers
  • Fixed trailing whitespace issues in existing test files (StyleCop SA1028 compliance)

Reviewed changes

Copilot reviewed 27 out of 27 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
UseProperAssertMethodsFixer.cs Enhanced trivia preservation for argument replacement and insertion operations
UseOSConditionAttributeInsteadOfRuntimeCheckFixer.cs Added logic to remove blank lines after removing if statements
PreferAssertFailOverAlwaysFalseConditionsFixer.cs Preserved parenthesis tokens when creating new argument lists
DoNotNegateBooleanAssertionFixer.cs Preserved leading trivia when unnegating expressions
AssertionArgsShouldBePassedInCorrectOrderFixer.cs Used is null pattern and preserved separators when swapping arguments
Multiple test files Added multi-line indentation test cases and removed trailing whitespace

if (index >= 0 && index < block.Statements.Count - 1)
{
StatementSyntax nextStatement = block.Statements[index + 1];
SyntaxTriviaList cleanedTrivia = new(nextStatement.GetLeadingTrivia().Where(t => !t.IsKind(SyntaxKind.EndOfLineTrivia)));
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for removing blank lines is incorrect. This removes ALL end-of-line trivia from the leading trivia, which will incorrectly remove legitimate newlines if the next statement has multi-line leading trivia (e.g., multi-line comments). The intent appears to be to remove only the first blank line after the if statement. Consider removing only the first EndOfLineTrivia instead of all of them, or use SkipWhile to remove leading whitespace/newline pairs.

Suggested change
SyntaxTriviaList cleanedTrivia = new(nextStatement.GetLeadingTrivia().Where(t => !t.IsKind(SyntaxKind.EndOfLineTrivia)));
SyntaxTriviaList leadingTrivia = nextStatement.GetLeadingTrivia();
SyntaxTriviaList cleanedTrivia = leadingTrivia;
for (int i = 0; i < leadingTrivia.Count; i++)
{
if (leadingTrivia[i].IsKind(SyntaxKind.EndOfLineTrivia))
{
cleanedTrivia = leadingTrivia.RemoveAt(i);
break;
}
}

Copilot uses AI. Check for mistakes.
string s = "";
Assert.AreEqual(
"", s, "values should match");
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixed code places all three arguments on one line despite the original code being multi-line. This is inconsistent with other test cases in the PR where multi-line formatting is preserved. The expected behavior should maintain the original multi-line structure with proper indentation for each argument.

Suggested change
"", s, "values should match");
"",
s,
"values should match");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSTEST0037 code fix messes up whitespace / new lines

2 participants