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)

1 Like

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