meziantou / Meziantou.Analyzer

A Roslyn analyzer to enforce some good practices in C#.
MIT License
952 stars 49 forks source link

MA0165: Make interpolated string #764

Open DmitriyMaksimov opened 2 weeks ago

DmitriyMaksimov commented 2 weeks ago

Version of the Meziantou.Analyzer NuGet package

2.0.175

Rule Identifier

MA0165

Target Framework

net8.0

C# Language version

C# 10

Description

After upgrading to 2.0.175 from 2.0.173 getting the following errors

error MA0165: Make interpolated string (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0165.md) [/home/ec2-user/workspace/xxx.csproj::TargetFramework=net8.0]

Required change doesn't make much sense - the analyzer requires converting all strings to interpolated strings

image image

Also, the link https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0165.md is broken

Reproduction Steps

Minimal code:

    public class Query
    {
        [JsonPropertyName("string")]
        public string Str { get; set; }
        }

Other information

No response

meziantou commented 2 weeks ago

The default severity is Hidden (silent), which means that your IDE shouldn't report it in the error window or in the text editor.

This rule is only intended to help developers by providing a refactoring to easily convert a string to an interpolated string.

Nothing visible:

image

You get a refactoring (similar to the existing one, provided by roslyn, to convert to raw string)

For the documentation, I'll add it (and check why the CI didn't catch it)

MuffPotter commented 2 weeks ago

Same issue with Visual Studio 17.11.5: [assembly: AssemblyTitle("Any text here")]

Only using dotnet_diagnostic.MA0165.severity=suggestion fixed the compiler error

meziantou commented 2 weeks ago

dotnet_diagnostic.MA0165.severity=suggestion would make it visible in the IDE, which is not intended. I do not reproduce the issue on my side (using VS 17.12 preview 4).

fixed the compiler error

Which compiler error? Do you have an error when building the project?

DmitriyMaksimov commented 2 weeks ago

Thank you very much for a such quick reply, @meziantou! 🙏 And thank you for absolutely great project! Your analyzer is what I add to all C# projects - IMO it's "must have" 👍

You are right - I have the following in .editorconfig file

# default severity for all analyzer diagnostics
dotnet_analyzer_diagnostic.severity = error

It's why I'm getting errors. It's a bit surprising that the default has a higher priority than defined in the analyzer.

Adding the following .editorconfig

dotnet_diagnostic.MA0165.severity = none

resolves the issue.

BTW, Rider has the same code fix for a long time ("To string interpolation"), so for Rider users it's redundant (and confusing which one to use 😁). But for VS users, it's a great code fix (for such users, the severity should be set to silent)! 👍

image
DmitriyMaksimov commented 2 weeks ago

FYI: Rider is now free for non-commercial use - so you can use it for your project.

fforjan commented 3 days ago

Same for us, our default severity is error, came here so we had to do the dotnet_diagnostic.MA0165.severity = none