dotnet / roslynator

Roslynator is a set of code analysis tools for C#, powered by Roslyn.

Home Page:https://josefpihrt.github.io/docs/roslynator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Add option to exclude strings (verbatim, interpolated, raw) from RCS0056

FreeYourMinds opened this issue · comments

Story

Our team is developing an application, and for the UI feedback there are several texts that exceed our limit set for RCS0056.
I do not want to increase the maximum length further because of readability in pull requests and also, for performance reasons,
do not want to wrap the message lines causing concatination.

Suggestion

It would be convenient if there were an option which would either let me define an explicit length only applied to strings of any kind or would allow me to explicitly disable the rule RCS0056 just for strings

Adding an option to prevent certain lines from being reported as too long is definitely possible.

Could you provide some examples of lines that should be skipped by RCS0056? Thanks.

Sure.

Structured Logging

These kinds of templates would need an exclusion
(most of these messages are still inline, some of those have already been moved to usage with the LoggerMessageAttribute)

"The tracking number '{TrackingNumber}' for the specified unit with the prefix '{SpecifiedUnitIdPrefix}' for bypassing did not match the expected pattern. ErrorCode: {ErrorCode}", 

Full Code

        this.logger.LogError(
            "The tracking number '{TrackingNumber}' for the specified unit with the prefix '{SpecifiedUnitIdPrefix}' for bypassing did not match the expected pattern. ErrorCode: {ErrorCode}", 
            trackingNumber,
            specifiedUnitIdPrefix,
            ErrorCodes.BypassUnitTrackingNumberNotMatched);

with the LoggerMessageAttribute the code would look like this

    [LoggerMessage(EventId = EventIds.BypassTrackingNumberPrefixUnmatched, Level = LogLevel.Error, Message = "The tracking number '{TrackingNumber}' for the specified unit with the prefix '{SpecifiedUnitIdPrefix}' for bypassing did not match the expected pattern. ErrorCode: {ErrorCode}", )]
    private partial void LogUnitPrefixDidNotMatch(int trackingNumber, string specifiedUnitIdPrefix, int errorCode);

It would be best if the attribute could be kept single line, but if required could also be spread with one parameter per line.

Since all attribute usages are ascendant in the code, they are currently easily excluded with a #pragma warning disable statement and therefore are not so important to get an exclusion, but would be nice to have

UI Feedback

This is the associated message as an interpolated string which is used for user feedback in the UI, which also should be excludable

$"Could not bypass. The prefix '{specifiedUnitIdPrefix}' of the specified unit associated with the tracking number '{trackingNumber}' did not match the expected pattern. Please verify your input",

Full Code

        return new ErrorResult(
            ErrorCodes.BypassUnitTrackingNumberNotMatched,
            "The bypass tracking number did not match the expected pattern",
                $"Could not bypass. The prefix '{specifiedUnitIdPrefix}' of the specified unit associated with the tracking number '{trackingNumber}' did not match the expected pattern. Please verify your input",
            ProblemPageUrls.UnmatchedUnit);

I hope these examples are sufficient to clarify my scenario, let me know if you need additional information.
And already thanks for considering

        this.logger.LogError(
            "The tracking number '{TrackingNumber}' for the specified unit with the prefix '{SpecifiedUnitIdPrefix}' for bypassing did not match the expected pattern. ErrorCode: {ErrorCode}", 
            trackingNumber,
            specifiedUnitIdPrefix,
            ErrorCodes.BypassUnitTrackingNumberNotMatched);

Will be fixed.

    [LoggerMessage(EventId = EventIds.BypassTrackingNumberPrefixUnmatched, Level = LogLevel.Error, Message = "The tracking number '{TrackingNumber}' for the specified unit with the prefix '{SpecifiedUnitIdPrefix}' for bypassing did not match the expected pattern. ErrorCode: {ErrorCode}", )]
    private partial void LogUnitPrefixDidNotMatch(int trackingNumber, string specifiedUnitIdPrefix, int errorCode);

It would be best if the attribute could be kept single line, but if required could also be spread with one parameter per line.

Will be fixed (parameter with string literal must be on its own line).

        return new ErrorResult(
            ErrorCodes.BypassUnitTrackingNumberNotMatched,
            "The bypass tracking number did not match the expected pattern",
                $"Could not bypass. The prefix '{specifiedUnitIdPrefix}' of the specified unit associated with the tracking number '{trackingNumber}' did not match the expected pattern. Please verify your input",
            ProblemPageUrls.UnmatchedUnit);

Will be fixed (same as first case).

Perfekt! Thank you for the quick implementation.

...(parameter with string literal must be on its own line)

I already expected this to be a requirement 😜