2

重複の可能性:
これらの PMD ルールの理由は何ですか?

DD/DU 警告が表示されるのはなぜですか?

これが私のコードです:

// DD warning from PMD
public Object foo() {
  Object result = null;
  if (condition) {
    // code block, no accec to result
    result = newResult;
  }
  return result;
}
// DU warning from PMD
List<Object> data = new ArrayList<Object>(anotherList);
anotherList.remove(1);
// some other modification of anotherList
if (condition) {
  // some code. no access to data
  for (Object o : data) {
    // loop for original content of the list
  }
}

ここで何か問題がありますか?それともPMDのバグですか?これらの警告を無視できますか?

4

1 に答える 1

4

あなたの DD 異常は、バグの可能性を減らして、実際により良く書くことができます:

return condition? newResult : null;

または、構文に関してより保守的な場合は、

if (condition)
  return newResult;
return null;

2 番目の例では、data無条件で作成していますが、条件付きでのみ使用しています。に書き換える

if (condition) {
  List<Object> data = new ArrayList<>(anotherList);
  // or maybe just use anotherList without copying
  ...
}
else {
  anotherList.remove(1);
  // some other modifications of anotherList
}
于 2013-01-18T09:51:00.077 に答える