@@ -67,7 +67,7 @@ private static async Task<Document> RefactorAsync(
67
67
68
68
SemanticModel semanticModel = await document . GetSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
69
69
70
- ExpressionSyntax value = GetExpressionToInline ( localDeclaration , semanticModel , cancellationToken ) ;
70
+ ExpressionSyntax value = GetExpressionToInline ( localDeclaration , nextStatement , semanticModel , cancellationToken ) ;
71
71
72
72
StatementSyntax newStatement = GetStatementWithInlinedExpression ( nextStatement , value ) ;
73
73
@@ -93,7 +93,11 @@ private static async Task<Document> RefactorAsync(
93
93
return await document . ReplaceStatementsAsync ( statementsInfo , newStatements , cancellationToken ) . ConfigureAwait ( false ) ;
94
94
}
95
95
96
- private static ExpressionSyntax GetExpressionToInline ( LocalDeclarationStatementSyntax localDeclaration , SemanticModel semanticModel , CancellationToken cancellationToken )
96
+ private static ExpressionSyntax GetExpressionToInline (
97
+ LocalDeclarationStatementSyntax localDeclaration ,
98
+ StatementSyntax statement ,
99
+ SemanticModel semanticModel ,
100
+ CancellationToken cancellationToken )
97
101
{
98
102
VariableDeclarationSyntax variableDeclaration = localDeclaration . Declaration ;
99
103
@@ -114,20 +118,41 @@ private static ExpressionSyntax GetExpressionToInline(LocalDeclarationStatementS
114
118
{
115
119
expression = expression . Parenthesize ( ) ;
116
120
117
- ExpressionSyntax typeExpression = ( variableDeclaration . Type . IsVar )
118
- ? variableDeclaration . Variables [ 0 ] . Initializer . Value
119
- : variableDeclaration . Type ;
121
+ TypeSyntax type = variableDeclaration . Type ;
122
+ ITypeSymbol typeSymbol ;
120
123
121
- ITypeSymbol typeSymbol = semanticModel . GetTypeSymbol ( typeExpression , cancellationToken ) ;
124
+ if ( type . IsVar )
125
+ {
126
+ typeSymbol = semanticModel . GetTypeSymbol ( variableDeclaration . Variables [ 0 ] . Initializer . Value , cancellationToken ) ! ;
127
+ type = typeSymbol . ToTypeSyntax ( ) . WithSimplifierAnnotation ( ) ;
128
+ }
129
+ else
130
+ {
131
+ typeSymbol = semanticModel . GetTypeSymbol ( type , cancellationToken ) ! ;
132
+ }
122
133
123
- if ( typeSymbol . SupportsExplicitDeclaration ( ) )
134
+ bool ShouldAddCast ( )
124
135
{
125
- TypeSyntax type = typeSymbol . ToMinimalTypeSyntax ( semanticModel , localDeclaration . SpanStart ) ;
136
+ if ( ! typeSymbol . SupportsExplicitDeclaration ( ) )
137
+ return false ;
138
+
139
+ if ( statement . IsKind ( SyntaxKind . ReturnStatement ) )
140
+ {
141
+ IMethodSymbol enclosingSymbol = semanticModel . GetEnclosingSymbol < IMethodSymbol > ( variableDeclaration . Type . SpanStart , cancellationToken ) ;
142
+
143
+ if ( enclosingSymbol is not null
144
+ && SymbolEqualityComparer . Default . Equals ( typeSymbol , enclosingSymbol . ReturnType ) )
145
+ {
146
+ return false ;
147
+ }
148
+ }
126
149
127
- expression = SyntaxFactory . CastExpression ( type , expression ) . WithSimplifierAnnotation ( ) ;
150
+ return true ;
128
151
}
129
152
130
- return expression ;
153
+ return ( ShouldAddCast ( ) )
154
+ ? SyntaxFactory . CastExpression ( type . WithoutTrivia ( ) , expression ) . WithSimplifierAnnotation ( )
155
+ : expression ;
131
156
}
132
157
}
133
158
0 commit comments