Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java Logger with Supplier<String>: @UnknownKeyFor String instead of @KeyForBottom String #2676

Closed
bmarwell opened this issue Aug 2, 2019 · 4 comments
Assignees
Milestone

Comments

@bmarwell
Copy link

bmarwell commented Aug 2, 2019

Hi again,

  • Java 8 (tested multiple JDKs)
  • Checker 2.10.0

I got the following code snippet:

  private static final java.util.logging.Logger LOGGER = Logger.getLogger("Query");
  // … enter method
  String query = String.format("%s", myList.stream().collect(joining(", ")));
  LOGGER.log(Level.FINEST, () -> String.format("Query [%s].", query));

This results in:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project my-project: Compilation failure: Compilation failure: 
[ERROR] Query.java:[4,27] error: [return.type.incompatible] incompatible types in return.
[ERROR]   type of expression: @UnknownKeyFor String
[ERROR]   method return type: @KeyForBottom String

Those are both Map-related annotations, and I have no idea what Map-related annotations are doing in my logging statement.
Haven’t found this combination in the issues or docs either.

Thanks in advance :)

@mernst
Copy link
Member

mernst commented Aug 2, 2019

Here is a complete reproduction for the problem: run

javacheck -g KeyForTest.java -processor nullness

on the following file.

import java.util.logging.Level;
import java.util.logging.Logger;

public class KeyForTest {

  Logger LOGGER = Logger.getLogger("Query");

  void test() {
    LOGGER.log(Level.FINEST, () -> String.format("%s", "hello, world"));
  }
}

As noted, the output is

KeyForTest.java:12: error: [return.type.incompatible] incompatible types in return.
    LOGGER.log(Level.FINEST, () -> String.format("%s", "hello, world"));
                                                ^
  type of expression: @UnknownKeyFor String
  method return type: @KeyForBottom String
1 error

@smillst smillst self-assigned this Aug 9, 2019
@smillst smillst added this to the Medium milestone Aug 9, 2019
@roded
Copy link

roded commented Oct 30, 2019

We're seeing the same issue on JUL logger methods. At the moment we've suppressed the keyFor check via a javac argument. Please look into this, it is somewhat preventing us from adopting NullnessChecker more widely.

P.S. We've enabled only the NullnessChecker, is keyFor entailed in Nullness checking?

@bmarwell
Copy link
Author

Workaround: create another stub for the logger methods.

@smillst
Copy link
Member

smillst commented May 21, 2024

This error is no longer issued.

@smillst smillst closed this as completed May 21, 2024
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

No branches or pull requests

4 participants