Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
Require that methods such as Pair.forEach and
MatcherAssert.assertThat are used via static import, and
enforce in LintTest.

Require use of
org.checkerframework.checker.nullness.qual.Nullable rather
than javax.annotation.Nullable, and similarly NonNull.
  • Loading branch information
julianhyde committed Jan 31, 2024
1 parent 071fc45 commit 9c3425d
Show file tree
Hide file tree
Showing 35 changed files with 149 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/hydromatic/morel/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Runnables;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
Expand Down Expand Up @@ -71,7 +72,6 @@
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.annotation.Nonnull;

import static net.hydromatic.morel.util.Static.str;

Expand Down Expand Up @@ -277,7 +277,7 @@ public void run() {
/** Instantiates a class.
*
* <p>Assumes that the class has a public no-arguments constructor. */
@Nonnull private static <T> T instantiate(String className,
@NonNull private static <T> T instantiate(String className,
@SuppressWarnings("SameParameterValue") Class<T> clazz) {
try {
final Class<?> aClass = Class.forName(className);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/hydromatic/morel/ast/Ast.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public Pat accept(Shuttle shuttle) {

@Override AstWriter unparse(AstWriter w, int left, int right) {
w.append("{");
Ord.forEachIndexed(args, (i, k, v) ->
Ord.forEachIndexed(args, (i, k, v) -> // lint:skip
w.append(i > 0 ? ", " : "").append(k).append(" = ").append(v, 0, 0));
if (ellipsis) {
w.append(args.isEmpty() ? "..." : ", ...");
Expand Down Expand Up @@ -642,7 +642,7 @@ public RecordType accept(Shuttle shuttle) {

AstWriter unparse(AstWriter w, int left, int right) {
w.append("{");
Ord.forEachIndexed(fieldTypes, (i, field, type) ->
Ord.forEachIndexed(fieldTypes, (i, field, type) -> // lint:skip
w.append(i > 0 ? ", " : "")
.id(field).append(": ").append(type, 0, 0));
return w.append("}");
Expand Down Expand Up @@ -1210,7 +1210,7 @@ public Exp accept(Shuttle shuttle) {

@Override AstWriter unparse(AstWriter w, int left, int right) {
w.append("{");
Ord.forEachIndexed(args, (i, k, v) ->
Ord.forEachIndexed(args, (i, k, v) -> // lint:skip
w.append(i > 0 ? ", " : "").append(k).append(" = ").append(v, 0, 0));
return w.append("}");
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/hydromatic/morel/ast/AstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.annotation.Nullable;

/** Builds parse tree nodes. */
public enum AstBuilder {
Expand Down Expand Up @@ -340,7 +340,7 @@ public Ast.From from(Pos pos, List<Ast.FromStep> steps) {
}

public Ast.From from(Pos pos, List<Ast.FromStep> steps,
@Nullable Ast.Exp implicitYieldExp) {
Ast.@Nullable Exp implicitYieldExp) {
return new Ast.From(pos, ImmutableList.copyOf(steps), implicitYieldExp);
}

Expand Down Expand Up @@ -369,7 +369,7 @@ public Ast.FunBind funBind(Pos pos,
}

public Ast.FunMatch funMatch(Pos pos, String name,
Iterable<? extends Ast.Pat> patList, @Nullable Ast.Type returnType,
Iterable<? extends Ast.Pat> patList, Ast.@Nullable Type returnType,
Ast.Exp exp) {
return new Ast.FunMatch(pos, name, ImmutableList.copyOf(patList),
returnType, exp);
Expand Down Expand Up @@ -458,7 +458,7 @@ public Ast.Exp map(Pos pos, Ast.Exp e1, Ast.Exp e2) {
}

public Ast.Scan scan(Pos pos, Ast.Pat pat, Ast.Exp exp,
@Nullable Ast.Exp condition) {
Ast.@Nullable Exp condition) {
return new Ast.Scan(pos, pat, exp, condition);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/hydromatic/morel/ast/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/
package net.hydromatic.morel.ast;

import java.util.Objects;
import static java.util.Objects.requireNonNull;

/** Abstract syntax tree node. */
public abstract class AstNode {
public final Pos pos;
public final Op op;

public AstNode(Pos pos, Op op) {
this.pos = Objects.requireNonNull(pos);
this.op = Objects.requireNonNull(op);
this.pos = requireNonNull(pos);
this.op = requireNonNull(op);
}

/** Converts this node into an ML string.
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/net/hydromatic/morel/ast/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -51,7 +52,6 @@
import java.util.Set;
import java.util.SortedMap;
import java.util.function.ObjIntConsumer;
import javax.annotation.Nullable;

import static net.hydromatic.morel.ast.CoreBuilder.core;
import static net.hydromatic.morel.util.Ord.forEachIndexed;
Expand Down Expand Up @@ -478,9 +478,10 @@ public static class RecordPat extends Pat {

@Override AstWriter unparse(AstWriter w, int left, int right) {
w.append("{");
forEachIndexed(type().argNameTypes.keySet(), args, (i, name, arg) ->
w.append(i > 0 ? ", " : "").append(name)
.append(" = ").append(arg, 0, 0));
forEachIndexed(type().argNameTypes.keySet(), args,
(i, name, arg) ->
w.append(i > 0 ? ", " : "").append(name)
.append(" = ").append(arg, 0, 0));
return w.append("}");
}

Expand Down Expand Up @@ -1428,10 +1429,10 @@ public static class Group extends FromStep {
@Override protected AstWriter unparse(AstWriter w, From from, int ordinal,
int left, int right) {
w.append(" group");
Pair.forEachIndexed(groupExps, (i, id, exp) ->
Pair.forEachIndexed(groupExps, (i, id, exp) -> // lint:skip
w.append(i == 0 ? " " : ", ")
.append(id, 0, 0).append(" = ").append(exp, 0, 0));
Pair.forEachIndexed(aggregates, (i, name, aggregate) ->
Pair.forEachIndexed(aggregates, (i, name, aggregate) -> // lint:skip
w.append(i == 0 ? " compute " : ", ")
.append(name, 0, 0).append(" = ").append(aggregate, 0, 0));
return w;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/hydromatic/morel/ast/FromBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.hydromatic.morel.compile.RefChecker;
import net.hydromatic.morel.type.Binding;
import net.hydromatic.morel.type.TypeSystem;
import net.hydromatic.morel.util.Pair;
import net.hydromatic.morel.util.PairList;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -174,7 +173,7 @@ && getLast(((Core.From) exp).steps).bindings.size() == 1
bindings = null;
} else if (pat instanceof Core.TuplePat) {
final Core.TuplePat tuplePat = (Core.TuplePat) pat;
Pair.forEach(tuplePat.args, lastStep.bindings,
forEach(tuplePat.args, lastStep.bindings,
(arg, binding) ->
nameExps.add(((Core.IdPat) arg).name, core.id(binding.id)));
bindings = null;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/hydromatic/morel/ast/Pos.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Nonnull;

/** Position of a parse-tree node. */
public class Pos {
Expand Down Expand Up @@ -221,7 +220,7 @@ public Pos plusAll(Iterable<Pos> poses) {
return sum(poses, startLine, startColumn, endLine, endColumn);
}

public Pos plusAll(@Nonnull List<? extends AstNode> nodes) {
public Pos plusAll(@NonNull List<? extends AstNode> nodes) {
//noinspection StaticPseudoFunctionalStyleMethod,ConstantConditions
return plusAll(Lists.transform(nodes, (AstNode node) -> node.pos));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/hydromatic/morel/compile/BuiltIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
Expand All @@ -42,7 +43,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import javax.annotation.Nonnull;

import static net.hydromatic.morel.type.PrimitiveType.BOOL;
import static net.hydromatic.morel.type.PrimitiveType.CHAR;
Expand Down Expand Up @@ -1645,7 +1645,7 @@ public enum BuiltIn {
}

BuiltIn(@Nullable String structure, String mlName,
@Nonnull PrimitiveType preferredType,
@NonNull PrimitiveType preferredType,
Function<TypeSystem, Type> typeFunction) {
this(structure, mlName, null, typeFunction, preferredType, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.util.JsonBuilder;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
Expand All @@ -77,7 +78,6 @@
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Predicate;
import javax.annotation.Nonnull;

import static net.hydromatic.morel.ast.CoreBuilder.core;
import static net.hydromatic.morel.util.Ord.forEachIndexed;
Expand Down Expand Up @@ -784,7 +784,7 @@ private static RelContext getRelContext(RelContext cx, Environment env,
* <p>Future work: rather than resolving by name, look up aggregate function
* in environment, and compare with standard implementation of "sum" etc.;
* support aggregate functions defined by expressions (e.g. lambdas). */
@Nonnull private SqlAggFunction aggOp(Core.Exp aggregate) {
@NonNull private SqlAggFunction aggOp(Core.Exp aggregate) {
if (aggregate instanceof Core.Literal) {
switch (((Core.Literal) aggregate).unwrap(BuiltIn.class)) {
case RELATIONAL_SUM:
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/hydromatic/morel/compile/Inliner.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.hydromatic.morel.type.FnType;
import net.hydromatic.morel.type.PrimitiveType;
import net.hydromatic.morel.type.TypeSystem;
import net.hydromatic.morel.util.Pair;

import com.google.common.collect.ImmutableMap;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -38,6 +37,7 @@
import java.util.Map;

import static net.hydromatic.morel.ast.CoreBuilder.core;
import static net.hydromatic.morel.util.Pair.forEach;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -176,7 +176,7 @@ public static Inliner of(TypeSystem typeSystem, Environment env,
&& tuplePat.args.stream().allMatch(arg -> arg.op == Op.ID_PAT)) {
final ImmutableMap.Builder<Core.Id, Core.Id> builder =
ImmutableMap.builder();
Pair.forEach(tuple.args, tuplePat.args, (arg, pat) ->
forEach(tuple.args, tuplePat.args, (arg, pat) ->
builder.put(core.id((Core.IdPat) pat), (Core.Id) arg));
return builder.build();
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/net/hydromatic/morel/compile/Pretty.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import net.hydromatic.morel.util.Ord;

import com.google.common.collect.Iterables;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.List;
import javax.annotation.Nonnull;

import static net.hydromatic.morel.parse.Parsers.appendId;
import static net.hydromatic.morel.util.Pair.forEachIndexed;
Expand All @@ -62,17 +62,17 @@ class Pretty {
}

/** Prints a value to a buffer. */
StringBuilder pretty(@Nonnull StringBuilder buf,
@Nonnull Type type, @Nonnull Object value) {
StringBuilder pretty(@NonNull StringBuilder buf,
@NonNull Type type, @NonNull Object value) {
int lineEnd = lineWidth < 0 ? -1 : (buf.length() + lineWidth);
return pretty1(buf, 0, new int[] {lineEnd}, 0, type, value, 0, 0);
}

/** Prints a value to a buffer. If the first attempt goes beyond
* {@code lineEnd}, back-tracks, adds a newline and indent, and
* tries again one time. */
private StringBuilder pretty1(@Nonnull StringBuilder buf, int indent,
int[] lineEnd, int depth, @Nonnull Type type, @Nonnull Object value,
private StringBuilder pretty1(@NonNull StringBuilder buf, int indent,
int[] lineEnd, int depth, @NonNull Type type, @NonNull Object value,
int leftPrec, int rightPrec) {
final int start = buf.length();
final int end = lineEnd[0];
Expand All @@ -96,15 +96,15 @@ private StringBuilder pretty1(@Nonnull StringBuilder buf, int indent,
return buf;
}

private static void indent(@Nonnull StringBuilder buf, int indent) {
private static void indent(@NonNull StringBuilder buf, int indent) {
for (int i = 0; i < indent; i++) {
buf.append(' ');
}
}

private StringBuilder pretty2(@Nonnull StringBuilder buf,
int indent, int[] lineEnd, int depth, @Nonnull Type type,
@Nonnull Object value, int leftPrec, int rightPrec) {
private StringBuilder pretty2(@NonNull StringBuilder buf,
int indent, int[] lineEnd, int depth, @NonNull Type type,
@NonNull Object value, int leftPrec, int rightPrec) {
if (value instanceof TypedVal) {
final TypedVal typedVal = (TypedVal) value;
final StringBuilder buf2 = new StringBuilder("val ");
Expand Down Expand Up @@ -378,9 +378,9 @@ private static Type unqualified(Type type) {
: type;
}

private StringBuilder printList(@Nonnull StringBuilder buf,
int indent, int[] lineEnd, int depth, @Nonnull Type elementType,
@Nonnull List<Object> list) {
private StringBuilder printList(@NonNull StringBuilder buf,
int indent, int[] lineEnd, int depth, @NonNull Type elementType,
@NonNull List<Object> list) {
buf.append("[");
int start = buf.length();
for (Ord<Object> o : Ord.zip(list)) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/hydromatic/morel/compile/TypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import net.hydromatic.morel.util.Tracers;
import net.hydromatic.morel.util.Unifier;

import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -81,6 +80,8 @@
import static net.hydromatic.morel.util.Static.transform;
import static net.hydromatic.morel.util.Static.transformEager;

import static com.google.common.base.Preconditions.checkArgument;

import static java.lang.String.join;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -1542,7 +1543,7 @@ private Resolved(Environment env,
this.originalNode = requireNonNull(originalNode);
this.node = requireNonNull(node);
this.typeMap = requireNonNull(typeMap);
Preconditions.checkArgument(originalNode instanceof Ast.FunDecl
checkArgument(originalNode instanceof Ast.FunDecl
? node instanceof Ast.ValDecl
: originalNode.getClass() == node.getClass());
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/hydromatic/morel/eval/EvalEnvs.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import javax.annotation.Nullable;

import static net.hydromatic.morel.util.Pair.allMatch;
import static net.hydromatic.morel.util.Pair.zip;
import static net.hydromatic.morel.util.Static.skip;

import static java.util.Objects.requireNonNull;

/** Helpers for {@link EvalEnv}. */
public class EvalEnvs {
/** Creates an evaluation environment with the given (name, value) map. */
Expand Down Expand Up @@ -99,8 +100,8 @@ static class ArraySubEvalEnv implements EvalEnv {

ArraySubEvalEnv(EvalEnv parentEnv, ImmutableList<String> names,
@Nullable Object[] values) {
this.parentEnv = Objects.requireNonNull(parentEnv);
this.names = Objects.requireNonNull(names);
this.parentEnv = requireNonNull(parentEnv);
this.names = requireNonNull(names);
this.values = values; // may be null
}

Expand Down Expand Up @@ -145,7 +146,7 @@ static class PatSubEvalEnv extends ArraySubEvalEnv {
PatSubEvalEnv(EvalEnv parentEnv, Core.Pat pat, ImmutableList<String> names,
Object[] values) {
super(parentEnv, ImmutableList.copyOf(names), values);
this.pat = Objects.requireNonNull(pat);
this.pat = requireNonNull(pat);
assert !(pat instanceof Core.IdPat);
}
}
Expand Down
Loading

0 comments on commit 9c3425d

Please sign in to comment.