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

**URL:** <https://community.sonarsource.com/t/s1854-remove-this-useless-assignment-to-local-variable-sourceenvid-with-lambdas/18307>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [January 4, 2020, 8:32pm UTC](https://community.sonarsource.com/t/s1854-remove-this-useless-assignment-to-local-variable-sourceenvid-with-lambdas/18307 "2020-01-04T20:32:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruslanbes](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ruslanbes/32/8494_2.png) [@ruslanbes](https://community.sonarsource.com/u/ruslanbes)\
**Post date:** [January 4, 2020, 8:32pm UTC](https://community.sonarsource.com/t/s1854-remove-this-useless-assignment-to-local-variable-sourceenvid-with-lambdas/18307/1 "2020-01-04T20:32:09Z")

</div>

- SonarJava 6.0
- SonarQube 7.9.1 LTS
- Sonar Maven Plugin: 3.4.0.905

```auto
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)

---

<div class="post-metadata">

**Author:** ![rbuer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/rbuer/32/8643_2.png) [@rbuer](https://community.sonarsource.com/u/rbuer)\
**Post date:** [January 15, 2020, 4:49pm UTC](https://community.sonarsource.com/t/s1854-remove-this-useless-assignment-to-local-variable-sourceenvid-with-lambdas/18307/2 "2020-01-15T16:49:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [January 30, 2020, 4:08pm UTC](https://community.sonarsource.com/t/s1854-remove-this-useless-assignment-to-local-variable-sourceenvid-with-lambdas/18307/3 "2020-01-30T16:08:53Z")

</div>

Hello @ruslanbes and @rbuer,

Indeed, this is an unexpected result, I created a [ticket](https://jira.sonarsource.com/browse/SONARJAVA-3281) to track this issue.

Thank you for the feedback and the reproducers.

Best,  
Quentin

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [July 24, 2020, 11:31am UTC](https://community.sonarsource.com/t/s1854-remove-this-useless-assignment-to-local-variable-sourceenvid-with-lambdas/18307/4 "2020-07-24T11:31:45Z")

</div>

Closed on July 24, 2020, 11:31am UTC

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