Browse Source

report some unused user variables (see #4744)

Simon Krajewski 9 years ago
parent
commit
09ada981fe
2 changed files with 20 additions and 1 deletions
  1. 16 0
      src/display/display.ml
  2. 4 1
      src/typing/typeload.ml

+ 16 - 0
src/display/display.ml

@@ -368,6 +368,22 @@ module Diagnostics = struct
 	open UnresolvedIdentifierSuggestion
 	open UnresolvedIdentifierSuggestion
 	open DiagnosticsKind
 	open DiagnosticsKind
 
 
+	let find_unused_variables com cf =
+		let vars = Hashtbl.create 0 in
+		let rec loop e = match e.eexpr with
+			| TVar(v,eo) when Meta.has Meta.UserVariable v.v_meta ->
+				Hashtbl.replace vars v.v_id v;
+				(match eo with None -> () | Some e -> loop e)
+			| TLocal v when Meta.has Meta.UserVariable v.v_meta ->
+				Hashtbl.remove vars v.v_id;
+			| _ ->
+				Type.iter loop e
+		in
+		(match cf.cf_expr with None -> () | Some e -> loop e);
+		Hashtbl.iter (fun _ v ->
+			add_diagnostics_message com "Unused variable" v.v_pos DiagnosticsSeverity.Warning
+		) vars
+
 	let print_diagnostics ctx =
 	let print_diagnostics ctx =
 		let com = ctx.com in
 		let com = ctx.com in
 		let diag = DynArray.create() in
 		let diag = DynArray.create() in

+ 4 - 1
src/typing/typeload.ml

@@ -3502,11 +3502,14 @@ let type_module ctx mpath file ?(is_extern=false) tdecls p =
 			List.iter (fun mt -> match mt with
 			List.iter (fun mt -> match mt with
 				| TClassDecl c | TAbstractDecl({a_impl = Some c}) ->
 				| TClassDecl c | TAbstractDecl({a_impl = Some c}) ->
 					ignore(c.cl_build());
 					ignore(c.cl_build());
-					let field cf = match cf.cf_kind with
+					let field cf =
+						begin match cf.cf_kind with
 						| Method MethMacro ->
 						| Method MethMacro ->
 							(try ignore (ctx.g.do_macro ctx MDisplay c.cl_path cf.cf_name [] p) with Exit -> ())
 							(try ignore (ctx.g.do_macro ctx MDisplay c.cl_path cf.cf_name [] p) with Exit -> ())
 						| _ ->
 						| _ ->
 							ignore(follow cf.cf_type);
 							ignore(follow cf.cf_type);
+						end;
+						Display.Diagnostics.find_unused_variables ctx.com cf;
 					in
 					in
 					List.iter field c.cl_ordered_fields;
 					List.iter field c.cl_ordered_fields;
 					List.iter field c.cl_ordered_statics;
 					List.iter field c.cl_ordered_statics;