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: False positive when guard is not a direct parent · Issue #4282 · pmd/pmd

[java] GuardLogStatement: False positive when guard is not a direct parent #4282

New issue
Copy link
New issue
Copy link
Closed
#6296
Closed
[java] GuardLogStatement: False positive when guard is not a direct parent#4282
#6296
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.20.0

Description

@Frettman
Frettman
opened on Jan 5, 2023
Issue body actions

Affects PMD Version:
6.53.0+
7.0.0

Rule: GuardLogStatement

Description:

I have an additional if between the guard method and the actual call to a log method (see example below). In the else I'm directly calling a (simple getter) method to provide an argument to the log method, which triggers the need for a guard. This raises a violation: Logger calls should be surrounded by log level guards.
The rule does not recognize that there's a guard around it, just not as direct parent in the syntax tree.

Code Sample demonstrating the issue:

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Foo {
    private static final Logger LOG = LogManager.getLogger(Foo.class);
    public void test(String arg1) {
        if (LOG.isWarnEnabled()) {
          if (someCondition) {
            String msg = "Message 1 with one argument: ";
            LOG.warn(msg + arg1);
          } else {
            String msg = "Message 2 with two arguments: {}, {}.";
            LOG.warn(msg + arg1);
          }
        }
    }
}

Expected outcome:

PMD should look further around it to detect a guard method.

Running PMD through: Maven | Eclipse Plug-in

Activity

  1. Frettman
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Jan 5, 2023
  2. adangel
    changed the title [-][java] GuardLogStatement only detects guard methods immediately around it.[/-] [+][java] GuardLogStatement only detects guard methods immediately around it[/+] on Jan 19, 2023
  3. jsotuyod
    added
    needs:pmd7-revalidationThe issue hasn't yet been retested vs PMD 7 and may be stale
    on Mar 17, 2024
  4. jsotuyod
    removed
    needs:pmd7-revalidationThe issue hasn't yet been retested vs PMD 7 and may be stale
    on Mar 25, 2024
  5. SheikhJebran-Philips commented on Jul 9, 2024

    @SheikhJebran-Philips
    SheikhJebran-Philips
    on Jul 9, 2024
    More actions

    facing the same issue

  6. krallus commented on Oct 3, 2024

    @krallus
    krallus
    on Oct 3, 2024
    More actions

    Interestingly, this issue does not occur for me in PMD 7.2, 7.3, or 7.4. However, it is a problem in PMD versions 7.5 and 7.6.

  7. adangel
    added this to the 7.20.0 milestone on Dec 21, 2025
  8. adangel
    changed the title [-][java] GuardLogStatement only detects guard methods immediately around it[/-] [+][java] GuardLogStatement: False positive when guard is not a direct parent[/+] on Dec 21, 2025
  9. adangel
    added a commit that references this issue on Dec 21, 2025

    [doc] Update release notes (#4282, #6296)

    f968a49
  10. adangel
    closed this as completedin d1bc5ddon Dec 21, 2025
  11. adangel
    added a commit that references this issue on Dec 21, 2025

    [java] Fix pmd#4282: GuardLogStatement only detects guard methods imm…

    This issue will close once commit d1bc5dd is merged into the 'main' branch.
    d1bc5dd
  12. wener-tiobe
    added a commit that references this issue on Feb 10, 2026

    RM-37163: CPD 7.21.0 upgrade (#8)

    6a1959a
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.20.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.