Skip to content

Conversation

noti0na1
Copy link
Member

@noti0na1 noti0na1 commented Sep 5, 2025

Report warning if a public method/field exposes FlexibleType in its result type under explicit nulls and encourage explicit annotation.

According to the discussion in #23682

def f(s: String) = s.trim // warn
-- Warning: Stest.scala:100:4 --------------------------------------------------
100 |def f(s: String) = s.trim // warn
    |    ^
    |Public method f exposes a flexible type (String)? in its inferred signature. Consider annotating the type as String or String | Null explicitly
1 warning found

@noti0na1 noti0na1 changed the title Add warnings for flexible types in public methods and fields Add warnings for inferred flexible types in public methods and fields Sep 5, 2025
@noti0na1 noti0na1 requested review from Copilot, sjrd and olhotak September 5, 2025 14:52
Copilot

This comment was marked as resolved.

@som-snytt
Copy link
Contributor

OT it would be interesting to infer the type (nullability) of an override from the RHS for an effectively final member. Then some explicit types would not be necessary when implementing Java interfaces.

@noti0na1 noti0na1 marked this pull request as ready for review September 7, 2025 17:10
@noti0na1 noti0na1 merged commit f24da5c into scala:main Sep 7, 2025
44 checks passed
@noti0na1 noti0na1 deleted the warn-public-flexible-types branch September 7, 2025 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants