File tree Expand file tree Collapse file tree
StyleCop.Analyzers.CodeFixes/MaintainabilityRules
StyleCop.Analyzers.Test/MaintainabilityRules Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -67,8 +67,11 @@ private static SyntaxNode GetReplacement(ParenthesizedExpressionSyntax oldNode)
6767 var leadingTrivia = SyntaxFactory . TriviaList ( oldNode . OpenParenToken . GetAllTrivia ( ) . Concat ( oldNode . Expression . GetLeadingTrivia ( ) ) ) ;
6868 var trailingTrivia = oldNode . Expression . GetTrailingTrivia ( ) . AddRange ( oldNode . CloseParenToken . GetAllTrivia ( ) ) ;
6969
70+ // Workaround for Roslyn not handling elastic markers for directive trivia correctly.
71+ var leadingSeparator = ( oldNode . Parent is DirectiveTriviaSyntax ) ? SyntaxFactory . Space : SyntaxFactory . ElasticMarker ;
72+
7073 return oldNode . Expression
71- . WithLeadingTrivia ( leadingTrivia . Any ( ) ? leadingTrivia : SyntaxFactory . TriviaList ( SyntaxFactory . ElasticMarker ) )
74+ . WithLeadingTrivia ( leadingTrivia . Any ( ) ? leadingTrivia : SyntaxFactory . TriviaList ( leadingSeparator ) )
7275 . WithTrailingTrivia ( trailingTrivia . Any ( ) ? trailingTrivia : SyntaxFactory . TriviaList ( SyntaxFactory . ElasticMarker ) ) ;
7376 }
7477
Original file line number Diff line number Diff line change @@ -1401,6 +1401,51 @@ public void Bar()
14011401 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
14021402 }
14031403
1404+ /// <summary>
1405+ /// Verifies that a preprocessor statement with unnecessary parenthesis is handled correctly.
1406+ /// Regression test for #2069
1407+ /// </summary>
1408+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1409+ [ Fact ]
1410+ public async Task VerifyThatPreprocessorStatementIsHandledCorrectlyAsync ( )
1411+ {
1412+ string testCode = @"
1413+ public class Program
1414+ {
1415+ public static void Main(string[] args)
1416+ {
1417+ #if(DEBUG)
1418+
1419+ #endif
1420+
1421+ }
1422+ }
1423+ " ;
1424+
1425+ string fixedCode = @"
1426+ public class Program
1427+ {
1428+ public static void Main(string[] args)
1429+ {
1430+ #if DEBUG
1431+
1432+ #endif
1433+
1434+ }
1435+ }
1436+ " ;
1437+ DiagnosticResult [ ] expected =
1438+ {
1439+ this . CSharpDiagnostic ( DiagnosticId ) . WithLocation ( 6 , 4 ) ,
1440+ this . CSharpDiagnostic ( ParenthesesDiagnosticId ) . WithLocation ( 6 , 4 ) ,
1441+ this . CSharpDiagnostic ( ParenthesesDiagnosticId ) . WithLocation ( 6 , 10 ) ,
1442+ } ;
1443+
1444+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
1445+ await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
1446+ await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
1447+ }
1448+
14041449 /// <inheritdoc/>
14051450 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
14061451 {
You can’t perform that action at this time.
0 commit comments