Allow unused method parameter when method is only used with method reference

Versions used: SonarQube 7.3

I have the following method signature:

public void registerNodeAnalyzer(BiConsumer<NaturalASTBaseNode, NaturalFileContext> analyzer, Class<? extends NaturalASTBaseNode> nodeType) 

which is then used within another class like this:

protected void initialize(LinterContext context) {
        context.registerStatementAnalyzer(this::analyzeOnErrorStatement, OnErrorStatement.class);
        context.registerNodeAnalyzer(this::analyzeEndError, NaturalASTTokenNode.class);
        context.registerNodeAnalyzer(this::findFirstNode, NaturalASTBaseNode.class);
        context.registerNodeAnalyzer(this::findFirstNode, NaturalASTTokenNode.class);
        context.registerNodeAnalyzer(this::findFirstNode, NaturalASTStatementNode.class);
        context.registerTokenAnalyzerById(this::findEndStatement, EToken.SK_END);
}

Now there is one functions which doesn’t use the second parameter of the BiConsumer (NaturalFileContext):

    private void analyzeEndError(NaturalASTBaseNode baseNode, NaturalFileContext context) {
        NaturalASTTokenNode tokenNode = (NaturalASTTokenNode) baseNode;
        ITokenForEditor token = tokenNode.getTokenForPosition();
        if (!containsOnError && token.getId().equals(EToken.SK_END_ERROR)) {
            containsOnError = true;
        }
    }

I get the issue

Unused method parameters should be removed

I can see why it is shown, but the only working workaround would be to wrap the registration into a lambda and not pass the context, so my code looks like

    protected void initialize(LinterContext context) {
        context.registerStatementAnalyzer(this::analyzeOnErrorStatement, OnErrorStatement.class);
        context.registerNodeAnalyzer((n, c) -> analyzeEndError(n), NaturalASTTokenNode.class);
        context.registerNodeAnalyzer(this::findFirstNode, NaturalASTBaseNode.class);
        context.registerNodeAnalyzer(this::findFirstNode, NaturalASTTokenNode.class);
        context.registerNodeAnalyzer(this::findFirstNode, NaturalASTStatementNode.class);
        context.registerTokenAnalyzerById(this::findEndStatement, EToken.SK_END);
    }

    private void analyzeEndError(NaturalASTBaseNode baseNode) {
        NaturalASTTokenNode tokenNode = (NaturalASTTokenNode) baseNode;
        ITokenForEditor token = tokenNode.getTokenForPosition();
        if (!containsOnError && token.getId().equals(EToken.SK_END_ERROR)) {
            containsOnError = true;
        }
    }

I find that less readable, because within the registration there is now one call which looks different (not using method reference) just to get rid of an issue.

Maybe the Java analyzer can figure out a way to not produce the issue in such an edge case, because omitting the parameter in the method signature breaks the code (because the method reference isn’t valid anymore)

Hi,

Good point, ticket is created.
Thanks for the feedback.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.