Menu

#1349 RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED does not work for Map

3.0.1
closed-fixed
None
5
2015-01-22
2015-01-20
derbeth
No

RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED works fine for ConcurrentMap. However, in JDK 8 the method putIfAbsent() has been added to Map interface. Findbugs 3.0.0 does not report this bug if Map is used.

Example:

import java.util.Calendar;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class CalendarUpdater {
    private final ConcurrentMap<String, Calendar> calendars = new ConcurrentHashMap<>();
    private final Map<String, Calendar> newCalendars = new ConcurrentHashMap<>();

    public Calendar update(String key, Calendar newValue) {
        calendars.putIfAbsent(key, newValue);
        newValue.add(Calendar.DAY_OF_MONTH, 2);
        return newValue;
    }

    public Calendar newUpdate(String key, Calendar newValue) {
        newCalendars.putIfAbsent(key, newValue);
        newValue.add(Calendar.DAY_OF_MONTH, 2);
        return newValue;
    }
}

Expected: RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED reported for update() and newUpdate()
Actual: bug reported only for update()

Discussion

  • Tagir Valeev

    Tagir Valeev - 2015-01-22

    Fixed, thanks. If FindBugs TypeAnalysis can determine that actual field/variable type is derived from ConcurrentMap, then the warning will be issued. I think ignoring putIfAbsent result for non-concurrent maps is ok.

    https://code.google.com/p/findbugs/source/detail?r=31db7004aea4ac45898e3ec7bdf7d40ff93221b1

     
  • Tagir Valeev

    Tagir Valeev - 2015-01-22
    • status: open --> closed-fixed
    • assigned_to: Tagir Valeev
    • Group: 3.x --> 3.0.1
     

Log in to post a comment.