Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Sonarlint complains about unused assignment/local variables when using string interpolation #25

Open
CC007 opened this issue Apr 26, 2024 · 2 comments

Comments

@CC007
Copy link

CC007 commented Apr 26, 2024

In this code snippet:

default String getTemplateFileName(HttpServletRequest request, Model model) {
    URI baseUri = getBaseUri();
    String templateFileName = this.getClass().getSimpleName() + ".ftl";
    String canonicalFileName = getResource(templateFileName)
            .map(baseUri::relativize)
            .map(URI::getPath)
            .orElse(COMPONENT_TEMPLATE_PREFIX + "content/error/Status500.ftl");
    log.info("Template name: $canonicalFileName");
    return "/$canonicalFileName";
}

The canonicalFileName is used in 2 places, however it is ONLY used in string interpolation. SonarLint doesn't understand how manifold added string interpolation, so it is giving a false positive that canonicalFileName is an unused local variable. Because of this, it also erroneously detects that the assignment to that variable is unused.

I don't know if I should file this bug here or on the SonarLint side. I think that the solution might be a combination of both.

@rsmckinney
Copy link
Member

As an IntelliJ plugin the invalid errors from Sonarlint may be suppressed from the manifold plugin. If Sonarlint is a JDK tool, not much can be done on my part if it isn't using the javac AST for analysis, which is fully formed with interpolation from manifold. I'll have a deeper look at sonarlist at some point.

@CC007
Copy link
Author

CC007 commented May 1, 2024

As an IntelliJ plugin the invalid errors from Sonarlint may be suppressed from the manifold plugin.

Well, good thing that SonarLint is a plugin then: https://plugins.jetbrains.com/plugin/7973-sonarlint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants