Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.SimplifyInterpolation;
Expand All @@ -19,6 +20,30 @@ public partial class SimplifyInterpolationTests : AbstractCSharpDiagnosticProvid
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpSimplifyInterpolationDiagnosticAnalyzer(), new CSharpSimplifyInterpolationCodeFixProvider());

[Fact]
public async Task SubsequentUnnecessarySpansDoNotRepeatTheSmartTag()
{
var parameters = new TestParameters(retainNonFixableDiagnostics: true, includeDiagnosticsOutsideSelection: true);

using var workspace = CreateWorkspaceFromOptions(@"class C
{
void M(string someValue)
{
_ = $""prefix {someValue{|Unnecessary:[||].ToString()|}{|Unnecessary:.PadLeft(|}3{|Unnecessary:)|}} suffix"";
}
}", parameters);

var diagnostics = await GetDiagnosticsWorkerAsync(workspace, parameters);

Assert.Equal(
new[] {
("IDE0071", DiagnosticSeverity.Info),
("IDE0071WithoutSuggestion", DiagnosticSeverity.Hidden),
("IDE0071WithoutSuggestion", DiagnosticSeverity.Hidden),
},
diagnostics.Select(d => (d.Descriptor.Id, d.Severity)));
}

[Fact]
public async Task ToStringWithNoParameter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.EmbeddedLanguages.VirtualChars;
Expand Down Expand Up @@ -63,18 +64,19 @@ private void AnalyzeInterpolation(OperationAnalysisContext context)
return;
}

var locations = ImmutableArray.Create(interpolation.Syntax.GetLocation());
context.ReportDiagnostic(DiagnosticHelper.Create(
UnnecessaryWithSuggestionDescriptor,
unnecessaryLocations.First(),
option.Notification.Severity,
additionalLocations: ImmutableArray.Create(interpolation.Syntax.GetLocation()),
properties: null));

var severity = option.Notification.Severity;

for (var i = 0; i < unnecessaryLocations.Length; i++)
// We start at 1 because the 0th element was used above to make the main diagnostic descriptor.
// All the rest are used to just fade out the correct portions of the user's code.
for (var i = 1; i < unnecessaryLocations.Length; i++)
{
context.ReportDiagnostic(DiagnosticHelper.Create(
i == 0 ? UnnecessaryWithSuggestionDescriptor : UnnecessaryWithoutSuggestionDescriptor,
unnecessaryLocations[i],
severity,
additionalLocations: locations,
properties: null));
context.ReportDiagnostic(Diagnostic.Create(
UnnecessaryWithoutSuggestionDescriptor, unnecessaryLocations[i]));
}
}
}
Expand Down