S1854: Remove this useless assignment to local variable "sourceEnvId" with lambdas

  • SonarJava 6.0
  • SonarQube 7.9.1 LTS
  • Sonar Maven Plugin: 3.4.0.905
final Long sourceEnvId = (Long) calculateSourceEnvId(); // S1854 is triggered

final Env sourceEnv = (Env) getEnvironments()
                .stream()
                .filter(env -> env.getId().equals(sourceEnvId)) 
                .findFirst()
                .orElse(null);

The variable sourceEnvId is used inside lambda but despite that the S1854 is triggered (should happen only when the var was not used at all)

2 Likes

In this Implementation of org.springframework.mail.javamail.MimeMessagePreparator

return mimeMessage -> {
  final MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
  messageHelper.setFrom(mailProperties.getUsername());
  final String replyToAddress = mailProperties.getReplyTo();
  if (!isBlank(replyToAddress)) {
    messageHelper.setReplyTo(replyToAddress);
  }
};

S1481 says that messageHelper and replyToAddress are unused.
Nowadays this pops up in old code. Due to that we are in an lambda too, this may corralate to original topic

Hello @ruslanbes and @rbuer,

Indeed, this is an unexpected result, I created a ticket to track this issue.

Thank you for the feedback and the reproducers.

Best,
Quentin

1 Like

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