-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7008 from JLLeitschuh/feat/JLL/java_optional_lamb…
…da_support Java: Model java.util.Optional lambda methods
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lgtm,codescanning | ||
* Added data flow models for lambda methods on `java.util.Optional`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import java.util.Optional; | ||
|
||
public class FunctionalTest { | ||
String source() { | ||
return null; | ||
} | ||
|
||
void sink(Object o) { | ||
} | ||
|
||
void test() { | ||
Optional<String> o = Optional.of(source()); | ||
o.ifPresent(v -> { | ||
sink(v); // $hasValueFlow | ||
}); | ||
o.ifPresentOrElse(v -> { | ||
sink(v); // $hasValueFlow | ||
}, () -> { | ||
// no-op | ||
}); | ||
o.map(v -> { | ||
sink(v); // $hasValueFlow | ||
return v; | ||
}).ifPresent(v -> { | ||
sink(v); // $hasValueFlow | ||
}); | ||
o.flatMap(v -> { | ||
sink(v); // $hasValueFlow | ||
return Optional.of(v); | ||
}).ifPresent(v -> { | ||
sink(v); // $hasValueFlow | ||
}); | ||
o.flatMap(v -> { | ||
sink(v); // $hasValueFlow | ||
return Optional.of("safe"); | ||
}).ifPresent(v -> { | ||
sink(v); // no value flow | ||
}); | ||
o.filter(v -> { | ||
sink(v); // $hasValueFlow | ||
return true; | ||
}).ifPresent(v -> { | ||
sink(v); // $hasValueFlow | ||
}); | ||
Optional.of("safe").map(v -> { | ||
sink(v); // no value flow | ||
return v; | ||
}).or(() -> o).ifPresent(v -> { | ||
sink(v); // $hasValueFlow | ||
}); | ||
Optional<String> safe = Optional.of("safe"); | ||
o.or(() -> safe).ifPresent(v -> { | ||
sink(v); // $hasValueFlow | ||
}); | ||
String value = safe.orElseGet(() -> source()); | ||
sink(value); // $hasValueFlow | ||
} | ||
} |