cheptsov / AdvancedExpressionFolding

https://plugins.jetbrains.com/plugin/9320?pr=idea
141 stars 30 forks source link

Wrong folding for this expression #71

Open ddimtirov opened 7 years ago

ddimtirov commented 7 years ago
if (value instanceof Collection) {
   return ((Collection<?>) value).stream().map(Column::defaultFormatter).collect(Collectors.joining(eol));
}

Folded to:

if (value instanceof Collection) {
   return value).stream().map(Column::defaultFormatter).collect(Collectors.joining(eol));
}

Note the dangling parenthesis after value.

jfcabral commented 7 years ago

Confirmed in v0.9.9.4 test snippet with 3 cases

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class Issue71 {
    public static void main(String[] args) {
        List<String> value = new ArrayList<>();
        if (value instanceof Collection) {
            // error #1 value)
            List<Object> a = ((Collection<?>) value).stream().map(Column::defaultFormatter).collect(Collectors.toList());
        }

        // error #2 first)
        Map<String, Object> first = new HashMap<String, Object>();
        final Object tableName = ((Map<String, ?>) first).get(".table");
    }

    public static BigDecimal toBigDecimal(final Integer value) {
        // error #3 value?)
        return value != null ? BigDecimal.valueOf(value) : null;
    }

    protected static class Column {
        static <R> R defaultFormatter(Object o) {
            return null;
        }
    }
}
jfcabral commented 6 years ago

Also confirmed in v0.9.9.7 with the edited snippet on the above comment ☝️