Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

PatternMatchingInstanceof misses casts with parenthesized operands

Aperta Adatta ai principianti
#6,215 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
74/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
testing, tooling

Direzione di ricerca

Inizia dal matcher PatternMatchingInstanceof e controlla come gestisce l’operando del cast; la issue suggerisce ASTHelpers.stripParentheses come possibile pista. Confronta i riproduttori forniti (String) o e (String)(o), poi aggiungi o esegui un test di regressione per entrambi. L’obiettivo è che entrambe le forme producano lo stesso suggerimento di pattern matching.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Error Prone version

Error Prone 2.49.0 (javac 21.0.8, --source/--target 21)

Affected checks

PatternMatchingInstanceof

Description

Adding redundant parentheses around a cast operand causes PatternMatchingInstanceof to miss an instanceof-and-cast pattern. The check suggests if (o instanceof String s) when the declaration uses (String) o, but no longer reports it when the declaration uses (String)(o).

Per JLS §15.8.5, parentheses around an expression do not change its value, type, or constant-expression status. The parentheses around the cast operand in JLS §15.16 do not change the cast's behavior, so the same pattern-matching suggestion should remain available.

I verified the following pair on Error Prone 2.49.0:

Check Rewrite BEFORE AFTER Effect
PatternMatchingInstanceof (String) o → (String)(o) 1 0 false negative

The matcher appears to inspect the cast operand without looking through ParenthesizedTree. A possible fix is to unwrap the operand before matching it, for example with ASTHelpers.stripParentheses.

Reproducers
PatternMatchingInstanceof — (String)(o) is missed

Per JLS §15.16 the parentheses around a cast operand are a no-op, but the instanceof-and-cast pattern is no longer recognized.

// BEFORE — 1 finding (suggests `if (o instanceof String s)`)
public class Sample {
  public int f(Object o) {
    if (o instanceof String) {
      String s = (String) o;
      return s.length();
    }
    return o.hashCode();
  }
}
// AFTER — 0 findings
public class Sample {
  public int f(Object o) {
    if (o instanceof String) {
      String s = (String)(o);
      return s.length();
    }
    return o.hashCode();
  }
}
Expected behavior

Both versions should produce 1 PatternMatchingInstanceof finding suggesting if (o instanceof String s). Parenthesizing o does not change the instanceof-and-cast pattern.

Actual behavior

The BEFORE version produces 1 finding suggesting pattern matching. The AFTER version produces 0 findings after the cast operand o is parenthesized.

Lingua principale
Java
Stelle
7.3k
Fork
827
Merge medio
12h 13m
PR unite (30g)
54

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/error-prone

Tutte le issue di google/error-prone

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.