GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] GuardLogStatement recognizes that a string is a compile-time constant expression only if at first position · Issue #5820 · pmd/pmd

[java] GuardLogStatement recognizes that a string is a compile-time constant expression only if at first position #5820

New issue
Copy link
New issue
Copy link
Closed
#6258
Closed
[java] GuardLogStatement recognizes that a string is a compile-time constant expression only if at first position#5820
#6258
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.19.0

Description

@frankk3
frankk3
opened on Jun 9, 2025
Issue body actions

Affects PMD Version: 7.14.0 (in fact since 7.5.0)

Rule: https://docs.pmd-code.org/pmd-doc-7.14.0/pmd_rules_java_bestpractices.html#guardlogstatement

Description:
After fixing the issue #3283 in PMD 6, PMD is able to recognize that a string is a compile-time constant expression and thus does not need to be guarded in log method calls.

While this detection also worked with PMD 6.45.0 until PMD 7.4.0 independent of the position of the compile-time constant expression in the method call, PMD 7.14.0 reports a violation (false positive) if the compile-time constant expression is not the first parameter, e.g. if a marker is the first parameter, or if the compile-time constant expression follows later on (more realistic use case than line 13 below: a specialized logger implementation is used that expects the message being built of long compile-time constant strings with line wrapping as third parameter).

Code Sample demonstrating the issue:

import org.slf4j.*;

public class MyLogger {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MyLogger.class);

	public static void main(String[] args) {
		LOGGER.debug("Part1" + " Part2"); // ok

		final Marker marker = MarkerFactory.getMarker("myMarker");
		LOGGER.debug(marker, "Part3 " + "Part4");  // false-positive GuardLogStatement
		
		LOGGER.debug(marker, "Message={}", "Part3 " +
		                                   "Part4");  // false-positive GuardLogStatement
	}
}

Expected outcome:

PMD reports a violation at line 11 as well as line 13, but that's wrong. That's a false positive.

Running PMD through: Gradle

Activity

  1. frankk3
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Jun 9, 2025
  2. adangel
    added this to the 7.19.0 milestone on Nov 27, 2025
  3. adangel
    added a commit that references this issue on Nov 27, 2025

    [doc] Update release notes (#5820, #6258)

    36a7fa8
  4. adangel
    closed this as completedin #6258on Nov 27, 2025
  5. adangel
    added a commit that references this issue on Nov 27, 2025

    [java] Fix #5820: GuardLogStatement recognizes that a string is a com…

    This issue will close once commit e8cd1de is merged into the 'main' branch.
    e8cd1de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.19.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.