Prechádzať zdrojové kódy

report unused catch-variables while pattern matching

Simon Krajewski 13 rokov pred
rodič
commit
3d20ee79ca

+ 24 - 12
matcher.ml

@@ -377,13 +377,13 @@ let to_pattern ctx e t =
 				error ((s_type (print_context()) t) ^ " should be Array") p)
 		| (EBinop(OpOr,(EBinop(OpOr,e1,e2),p2),e3),p1) ->
 			loop tctx (EBinop(OpOr,e1,(EBinop(OpOr,e2,e3),p2)),p1) t
-		| (EBinop(OpAssign,(EConst(Ident s),_),e1),p) ->
+		| (EBinop(OpAssign,(EConst(Ident s),p2),e1),p) ->
 			let v = mk_var tctx s t p in
 			let pat1 = loop tctx e1 t in
 			{
 				pdef = PatBind(v,pat1);
 				ptype = t;
-				ppos = p;
+				ppos = p2;
 			};
 		| (EBinop(OpOr,e1,e2),p) ->
 			let old = tctx.pc_locals in
@@ -510,11 +510,11 @@ let rec column_sigma mctx (st : subterm) (pmat : pattern_matrix) : ((con * bool)
 			| ({pdef=PatOr(pat1,pat2)} :: _),out ->
 				let acc1 = loop acc [[pat1],out] in
 				loop acc1 [[pat2],out]
-			| ({pdef=PatVar(SVar v,_)} :: _),out ->
-				bind_subterm out v st;
+			| ({pdef=PatVar(SVar v,_)} as pat :: _),out ->
+				bind_subterm out v (fst st,pat.ppos);
 				acc
-			| (({pdef=PatBind(v,pat)}) :: pl,out) ->
-				bind_subterm out v st;
+			| (({pdef=PatBind(v,pat)} as pat2) :: pl,out) ->
+				bind_subterm out v (fst st,pat2.ppos);
 				loop2 acc ((pat :: pl),out)
 			| _ ->
 				acc
@@ -529,11 +529,11 @@ let bind_remaining (out : outcome) (stl : subterm list) (row : pattern list) =
 	let rec loop st pat = match st,pat with
 		| st :: stl,{pdef = PatAny} :: pl ->
 			loop stl pl
-		| st :: stl,{pdef = PatVar(SVar v,_)} :: pl ->
-			bind_subterm out v st;
+		| st :: stl,({pdef = PatVar(SVar v,_)} as pat) :: pl ->
+			bind_subterm out v (fst st, pat.ppos);
 			loop stl pl
-		| st :: stl,pat :: pl ->
-			loop ([st] @ stl) pl
+		| _ :: _,_ :: pl ->
+			loop st pl
 		| st :: stl,[] ->
 			()
 		| [],_ ->
@@ -643,18 +643,30 @@ let subterm_to_varname st =
 	String.concat "_s" (ExtString.String.nsplit (s_subterm st) ".")
 
 let replace_locals ctx out e =
+	let all_subterms = Hashtbl.create 0 in
 	let subst = List.map (fun (v,st) ->
 		let vt = PMap.find (subterm_to_varname st) ctx.locals in
+		Hashtbl.add all_subterms vt st;
 		v, vt
 	) out.o_bindings in
+	let replace v =
+		try
+			let v2 = List.assq v subst in
+			Hashtbl.remove all_subterms v2;
+			v2
+		with Not_found ->
+			v
+	in 
 	let rec loop e = match e.eexpr with
 		| TLocal v ->
-			let v = try List.assq v subst with Not_found -> v in
+			let v = replace v in
 			{ e with eexpr = TLocal v}
 		| _ ->
 			Type.map_expr loop e
 	in
-	loop e
+	let e = loop e in
+	Hashtbl.iter (fun _ st -> ctx.com.warning "This variable is unused" (pos st)) all_subterms;
+	e
 
 let mk_const ctx p = function
 	| TString s -> mk (TConst (TString s)) ctx.com.basic.tstring p

+ 1 - 1
std/haxe/Json.hx

@@ -133,7 +133,7 @@ class Json {
 				#else
 				objString(v);
 				#end
-		case TEnum(e):
+		case TEnum(_):
 			var i : Dynamic = Type.enumIndex(v);
 			add(i);
 		case TBool:

+ 2 - 2
std/haxe/xml/Check.hx

@@ -291,10 +291,10 @@ class Check {
 		case CMissingAttrib(att,x):
 			path.push(x);
 			return makeWhere(path)+"missing required attribute "+att;
-		case CInvalidAttrib(att,x,f):
+		case CInvalidAttrib(att,x,_):
 			path.push(x);
 			return makeWhere(path)+"invalid attribute value for "+att;
-		case CInvalidData(x,f):
+		case CInvalidData(x,_):
 			return makeWhere(path)+"invalid data format for "+makeString(x);
 		case CInElement(x,m):
 			path.push(x);

+ 4 - 4
std/sys/db/SpodMacros.hx

@@ -142,7 +142,7 @@ class SpodMacros {
 
 	function makeSpod( t : haxe.macro.Type ) {
 		switch( t ) {
-		case TInst(c, p):
+		case TInst(c, _):
 			var name = c.toString();
 			var cl = c.get();
 			var csup = cl.superClass;
@@ -183,7 +183,7 @@ class SpodMacros {
 
 	function makeType( t : haxe.macro.Type ) {
 		switch( t ) {
-		case TInst(c, p):
+		case TInst(c, _):
 			var name = c.toString();
 			return switch( name ) {
 			case "Int": DInt;
@@ -201,7 +201,7 @@ class SpodMacros {
 			case "Bool": DBool;
 			default: throw "Unsupported SPOD Type " + name;
 			}
-		case TEnum(e, p):
+		case TEnum(e, _):
 			var name = e.toString();
 			return switch( name ) {
 			case "Bool": DBool;
@@ -727,7 +727,7 @@ class SpodMacros {
 			case OpUShr, OpInterval, OpAssignOp(_), OpAssign:
 				error("Unsupported operation", p);
 			}
-		case EUnop(op, post, e):
+		case EUnop(op, _, e):
 			var r = buildCond(e);
 			switch( op ) {
 			case OpNot:

+ 3 - 3
tests/unit/TestMatch.hx

@@ -36,7 +36,7 @@ class TestMatch extends Test {
 				s;
 			case EArray(_, { expr : EConst(CInt(i) | CFloat(i)) } ):
 				Std.string(i);
-			case EIn(_, { expr : e, pos : p }) :
+			case EIn(_, { expr : e, pos : _ }) :
 				Std.string(e);
 			case _:
 				"not_found";
@@ -74,7 +74,7 @@ class TestMatch extends Test {
 			case [a, b]: "4:" + a + "," +b;
 			case a in a.length == 3: "5:" + a.length;
 			case []: "6";
-			case a: "7";
+			case _: "7";
 		}		
 	}
 	
@@ -197,7 +197,7 @@ class TestMatch extends Test {
 		return switch (x1) {
 			case U1(x) in x > 1: ">1";
 			case U1(x) in x <= 1: "<=1";
-			case U1(x): throw "this is impossible to reach actually";
+			case U1(_): throw "this is impossible to reach actually";
 			case U2: "U2";
 		}
 	}

+ 1 - 1
tests/unit/TestMeta.hx

@@ -70,7 +70,7 @@ package unit;
 
 	@:macro static function getMeta(e) {
 		switch(e.expr) {
-			case haxe.macro.Expr.ExprDef.EMeta(m, e):
+			case haxe.macro.Expr.ExprDef.EMeta(m, _):
 				return macro { name: $(m.name), args: $[m.params] };
 			default:
 				return macro report("Metadata expected");