2
0
Эх сурвалжийг харах

Detect field typos in switch (#10438)

* Detect field typos in switch

* enum abstract test

* lisp power
RblSb 4 жил өмнө
parent
commit
71363dd28b

+ 24 - 12
src/typing/matcher.ml

@@ -219,6 +219,16 @@ module Pattern = struct
 		let con_type_expr mt p = ConTypeExpr mt,p in
 		let con_type_expr mt p = ConTypeExpr mt,p in
 		let con_array i p = ConArray i,p in
 		let con_array i p = ConArray i,p in
 		let con_fields fl p = ConFields fl,p in
 		let con_fields fl p = ConFields fl,p in
+		let get_enumerable_idents () = match follow t with
+			| TEnum(en,_) ->
+				en.e_names
+			| TAbstract({a_impl = Some c} as a,pl) when a.a_enum ->
+				ExtList.List.filter_map (fun cf ->
+					if has_class_field_flag cf CfImpl && has_class_field_flag cf CfEnum then Some cf.cf_name else None
+				) c.cl_ordered_statics
+			| _ ->
+				[]
+		in
 		let check_expr e =
 		let check_expr e =
 			let rec loop e = match e.eexpr with
 			let rec loop e = match e.eexpr with
 				| TField(_,FEnum(en,ef)) ->
 				| TField(_,FEnum(en,ef)) ->
@@ -231,7 +241,19 @@ module Pattern = struct
 					PatConstructor(con_const ct e.epos,[])
 					PatConstructor(con_const ct e.epos,[])
 				| TCast(e1,None) ->
 				| TCast(e1,None) ->
 					loop e1
 					loop e1
-				| TField _ ->
+				| TField (ef,f) ->
+					let s = field_name f in
+					begin match StringError.get_similar s (get_enumerable_idents()) with
+						| [] -> ()
+						| l ->
+							let tpath = match follow t with
+								| TEnum (e,tl) -> s_type_path e.e_path ^ "."
+								| TAbstract (a,tl) -> s_type_path a.a_path ^ "."
+								| _ -> ""
+							in
+							let fields = List.map (fun (el) -> tpath ^ el) l in
+							pctx.ctx.com.warning ("Potential typo detected (expected similar values are " ^ (String.concat ", " fields) ^ ")") p
+					end;
 					raise (Bad_pattern "Only inline or read-only (default, never) fields can be used as a pattern")
 					raise (Bad_pattern "Only inline or read-only (default, never) fields can be used as a pattern")
 				| TTypeExpr mt ->
 				| TTypeExpr mt ->
 					PatConstructor(con_type_expr mt e.epos,[])
 					PatConstructor(con_type_expr mt e.epos,[])
@@ -292,17 +314,7 @@ module Pattern = struct
 					if not (is_lower_ident s) && (match s.[0] with '`' | '_' -> false | _ -> true) then begin
 					if not (is_lower_ident s) && (match s.[0] with '`' | '_' -> false | _ -> true) then begin
 						display_error ctx "Pattern variables must be lower-case" p;
 						display_error ctx "Pattern variables must be lower-case" p;
 					end;
 					end;
-					let sl = match follow t with
-						| TEnum(en,_) ->
-							en.e_names
-						| TAbstract({a_impl = Some c} as a,pl) when a.a_enum ->
-							ExtList.List.filter_map (fun cf ->
-								if has_class_field_flag cf CfImpl && has_class_field_flag cf CfEnum then Some cf.cf_name else None
-							) c.cl_ordered_statics
-						| _ ->
-							[]
-					in
-					begin match StringError.get_similar s sl with
+					begin match StringError.get_similar s (get_enumerable_idents()) with
 						| [] ->
 						| [] ->
 							()
 							()
 							(* if toplevel then
 							(* if toplevel then

+ 25 - 0
tests/misc/projects/Issue6938/Main1.hx

@@ -0,0 +1,25 @@
+enum ScreenMode<T> {
+	AAA;
+	AAB;
+}
+enum abstract ScreenRatio(Int) {
+	var ZZA;
+	var ZZB;
+}
+
+class Main {
+	static final AAF = 0;
+	static final ZZF = 0;
+	static function main() {
+		var mode = ScreenMode.AAA;
+		switch(mode) {
+			case ScreenMoze.AAZ:
+			case _:
+		}
+		var mode2 = ScreenRatio.ZZA;
+		switch(mode2) {
+			case ScreenRazio.ZZZ:
+			case _:
+		}
+	}
+}

+ 2 - 0
tests/misc/projects/Issue6938/compile1-fail.hxml

@@ -0,0 +1,2 @@
+--main Main1
+--interp

+ 4 - 0
tests/misc/projects/Issue6938/compile1-fail.hxml.stderr

@@ -0,0 +1,4 @@
+Main1.hx:16: characters 9-23 : Warning : Potential typo detected (expected similar values are ScreenMode.AAA, ScreenMode.AAB)
+Main1.hx:16: characters 9-23 : Only inline or read-only (default, never) fields can be used as a pattern
+Main1.hx:21: characters 9-24 : Warning : Potential typo detected (expected similar values are ScreenRatio.ZZA, ScreenRatio.ZZB)
+Main1.hx:21: characters 9-24 : Only inline or read-only (default, never) fields can be used as a pattern