Skip to content

Commit f22b350

Browse files
authored
Remove trailing trivia from IHeaderDictionary in "Use indexer" code fix (#57649)
* Remove trailing trivia from IHeaderDictionary * Improve test case comment
1 parent 71f0a94 commit f22b350

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Framework/AspNetCoreAnalyzers/src/CodeFixes/Http/HeaderDictionaryAddFixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private static bool CanReplaceWithIndexer(Diagnostic diagnostic, SyntaxNode root
124124
SyntaxFactory.AssignmentExpression(
125125
SyntaxKind.SimpleAssignmentExpression,
126126
SyntaxFactory.ElementAccessExpression(
127-
memberAccessExpression.Expression,
127+
memberAccessExpression.Expression.WithoutTrailingTrivia(),
128128
SyntaxFactory.BracketedArgumentList(
129129
SyntaxFactory.SeparatedList(new[] { arguments[0] }))),
130130
arguments[1].Expression);

src/Framework/AspNetCoreAnalyzers/test/Http/HeaderDictionaryAddTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,27 @@ await VerifyCS.VerifyCodeFixAsync(
232232
@"
233233
using Microsoft.AspNetCore.Http;
234234
235+
var context = new DefaultHttpContext();
236+
context.Request.Headers[""Accept""] = ""text/html""{|CS1002:|}"
237+
},
238+
239+
// With trailing trivia
240+
{
241+
@"
242+
using Microsoft.AspNetCore.Http;
243+
244+
var context = new DefaultHttpContext();
245+
{|#0:context.Request.Headers
246+
.Add(""Accept"", ""text/html"")|}{|CS1002:|}",
247+
new[]
248+
{
249+
new DiagnosticResult(DiagnosticDescriptors.DoNotUseIHeaderDictionaryAdd)
250+
.WithLocation(0)
251+
.WithMessage(Resources.Analyzer_HeaderDictionaryAdd_Message)
252+
},
253+
@"
254+
using Microsoft.AspNetCore.Http;
255+
235256
var context = new DefaultHttpContext();
236257
context.Request.Headers[""Accept""] = ""text/html""{|CS1002:|}"
237258
}

0 commit comments

Comments
 (0)