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.

if ctx.phase.isTyper && ctx.explicitNulls && !ctx.isJava
&& sym.exists && sym.isPublic && sym.owner.isClass
&& !sym.isOneOf(Synthetic | InlineProxy | Param) then
val resTp = sym.info.finalResultType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of a method, should we also warn if the parameter types are flexible? Or can that never happen for some reason?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible to infer parameter types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise this LGTM, but let's wait for the CI.

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.

3 participants