Skip to content

Commit

Permalink
Merge branch 'redundant-do-fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Bataev committed Aug 2, 2020
2 parents 61b542e + 9dc92a2 commit 4ec56b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
16 changes: 15 additions & 1 deletion core/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,17 @@ func parseDef(obj Object, ctx *ParseContext, isForLinter bool) *DefExpr {
}
}

func skipRedundantDo(obj Object) bool {
if meta, ok := obj.(Meta); ok {
if m := meta.GetMeta(); m != nil {
if ok, res := m.Get(MakeKeyword("skip-redundant-do")); ok {
return res.Equals(Boolean{B: true})
}
}
}
return false
}

func parseBody(seq Seq, ctx *ParseContext) []Expr {
recur := ctx.recur
ctx.recur = false
Expand All @@ -772,7 +783,7 @@ func parseBody(seq Seq, ctx *ParseContext) []Expr {
if LINTER_MODE {
if defExpr, ok := expr.(*DefExpr); ok && !defExpr.isCreatedByMacro {
printParseWarning(defExpr.Pos(), "inline def")
} else if doExpr, ok := expr.(*DoExpr); ok && !doExpr.isCreatedByMacro {
} else if doExpr, ok := expr.(*DoExpr); ok && !doExpr.isCreatedByMacro && !skipRedundantDo(ro) {
printParseWarning(doExpr.Pos(), "redundant do form")
}
}
Expand Down Expand Up @@ -1208,6 +1219,9 @@ func fixInfo(obj Object, info *ObjectInfo) Object {
s = s.Rest()
}
res := NewListFrom(objs...)
if s, ok := obj.(Meta); ok {
res.meta = s.GetMeta()
}
if objInfo := obj.GetInfo(); objInfo != nil {
return res.WithInfo(objInfo)
}
Expand Down
6 changes: 6 additions & 0 deletions core/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ func makeFnForm(args map[int]Symbol, body Object) Object {
for _, v := range a {
argVector = argVector.Conjoin(v)
}
if LINTER_MODE {
if meta, ok := body.(Meta); ok {
m := EmptyArrayMap().Plus(MakeKeyword("skip-redundant-do"), Boolean{B: true})
body = meta.WithMeta(m)
}
}
return DeriveReadObject(body, NewListFrom(MakeSymbol("joker.core/fn"), argVector, body))
}

Expand Down
2 changes: 2 additions & 0 deletions tests/linter/redundant-do/input.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@

(when-let [a 1]
(do 1 a))

#(do (println 1) (println 2))

0 comments on commit 4ec56b8

Please sign in to comment.