-
Notifications
You must be signed in to change notification settings - Fork 291
Ensure code fixers are handling indentation/newlines correctly #7235
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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))); |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
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.
| 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; | |
| } | |
| } |
| string s = ""; | ||
| Assert.AreEqual( | ||
| "", s, "values should match"); |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
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.
| "", s, "values should match"); | |
| "", | |
| s, | |
| "values should match"); |
Fixes #7204