Skip to content

Commit 87903e5

Browse files
Wilfredfacebook-github-bot
authored andcommitted
Add autofix for lint on UNSAFE_CAST
Summary: Offer an autofix when we know the best input type for the user to use. Also reword the lint message to be a little more concise. Reviewed By: vsiles Differential Revision: D39913756 fbshipit-source-id: fbf093f665e27b721c2bdca7d6fc4a53e676caad
1 parent 7687696 commit 87903e5

7 files changed

+45
-13
lines changed

hphp/hack/src/lints/lints_errors.ml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,18 +392,22 @@ let duplicate_property pos ~class_name ~prop_name ~class_names =
392392

393393
let loose_unsafe_cast_lower_bound p ty_str_opt =
394394
let msg =
395-
"HH\\FIXME\\UNSAFE_CAST input type annotation is too loose, please use a more specific type."
395+
"The input type to `HH\\FIXME\\UNSAFE_CAST` should be as specific as possible."
396396
in
397-
let msg =
397+
let (msg, autofix) =
398398
match ty_str_opt with
399399
| Some ty_str ->
400-
msg
401-
^ " The typechecker infers "
402-
^ Markdown_lite.md_codify ty_str
403-
^ " as the most specific type."
404-
| None -> msg
400+
let path = Pos.filename (Pos.to_absolute p) in
401+
let lines = Errors.read_lines path in
402+
let src = String.concat ~sep:"\n" lines in
403+
let original = Pos.get_text_from_pos ~content:src p in
404+
let (start_offset, end_offset) = Pos.info_raw p in
405+
let width = end_offset - start_offset in
406+
( msg ^ " Consider using " ^ Markdown_lite.md_codify ty_str ^ " instead.",
407+
Some (original, ty_str, start_offset, width) )
408+
| None -> (msg, None)
405409
in
406-
Lints.add Codes.loose_unsafe_cast_lower_bound Lint_error p msg
410+
Lints.add ~autofix Codes.loose_unsafe_cast_lower_bound Lint_error p msg
407411

408412
let loose_unsafe_cast_upper_bound p =
409413
Lints.add
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?hh
2+
3+
function nullable_str(): ?string {
4+
return null;
5+
}
6+
7+
function demo(): void {
8+
$_ = HH\FIXME\UNSAFE_CAST<mixed, string>(nullable_str());
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"errors":[
3+
{
4+
"descr":"The input type to `HH\\FIXME\\UNSAFE_CAST` should be as specific as possible. Consider using `?string` instead.",
5+
"severity":"error",
6+
"path":"test_quickfix_unsafe_cast.php",
7+
"line":8,
8+
"start":29,
9+
"end":33,
10+
"code":5636,
11+
"bypass_changed_lines":false,
12+
"original":"mixed",
13+
"replacement":"?string",
14+
"start_offset":111,
15+
"width":5
16+
}
17+
],
18+
"version":" Wed Dec 31 16:00:00 1969"
19+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
File "unsafe_cast_lower_bound2.php", line 4, characters 25-29:
2-
HH\FIXME\UNSAFE_CAST input type annotation is too loose, please use a more specific type. The typechecker infers `string` as the most specific type. (Lint[5636])
2+
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `string` instead. (Lint[5636])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
File "unsafe_cast_lower_bound3.php", line 4, characters 25-29:
2-
HH\FIXME\UNSAFE_CAST input type annotation is too loose, please use a more specific type. (Lint[5636])
2+
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. (Lint[5636])
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
File "unsafe_cast_lower_bound4.php", line 4, characters 25-29:
2-
HH\FIXME\UNSAFE_CAST input type annotation is too loose, please use a more specific type. The typechecker infers `arraykey` as the most specific type. (Lint[5636])
2+
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `arraykey` instead. (Lint[5636])
33
File "unsafe_cast_lower_bound4.php", line 8, characters 25-29:
4-
HH\FIXME\UNSAFE_CAST input type annotation is too loose, please use a more specific type. The typechecker infers `num` as the most specific type. (Lint[5636])
4+
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `num` instead. (Lint[5636])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
File "unsafe_cast_lower_bound5.php", line 4, characters 25-29:
2-
HH\FIXME\UNSAFE_CAST input type annotation is too loose, please use a more specific type. The typechecker infers `?int` as the most specific type. (Lint[5636])
2+
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `?int` instead. (Lint[5636])

0 commit comments

Comments
 (0)