Skip to content

Commit

Permalink
Add autofix for lint on UNSAFE_CAST
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Wilfred authored and facebook-github-bot committed Sep 29, 2022
1 parent 7687696 commit 87903e5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
20 changes: 12 additions & 8 deletions hphp/hack/src/lints/lints_errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,22 @@ let duplicate_property pos ~class_name ~prop_name ~class_names =

let loose_unsafe_cast_lower_bound p ty_str_opt =
let msg =
"HH\\FIXME\\UNSAFE_CAST input type annotation is too loose, please use a more specific type."
"The input type to `HH\\FIXME\\UNSAFE_CAST` should be as specific as possible."
in
let msg =
let (msg, autofix) =
match ty_str_opt with
| Some ty_str ->
msg
^ " The typechecker infers "
^ Markdown_lite.md_codify ty_str
^ " as the most specific type."
| None -> msg
let path = Pos.filename (Pos.to_absolute p) in
let lines = Errors.read_lines path in
let src = String.concat ~sep:"\n" lines in
let original = Pos.get_text_from_pos ~content:src p in
let (start_offset, end_offset) = Pos.info_raw p in
let width = end_offset - start_offset in
( msg ^ " Consider using " ^ Markdown_lite.md_codify ty_str ^ " instead.",
Some (original, ty_str, start_offset, width) )
| None -> (msg, None)
in
Lints.add Codes.loose_unsafe_cast_lower_bound Lint_error p msg
Lints.add ~autofix Codes.loose_unsafe_cast_lower_bound Lint_error p msg

let loose_unsafe_cast_upper_bound p =
Lints.add
Expand Down
9 changes: 9 additions & 0 deletions hphp/hack/test/json/test_quickfix_unsafe_cast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?hh

function nullable_str(): ?string {
return null;
}

function demo(): void {
$_ = HH\FIXME\UNSAFE_CAST<mixed, string>(nullable_str());
}
19 changes: 19 additions & 0 deletions hphp/hack/test/json/test_quickfix_unsafe_cast.php.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"errors":[
{
"descr":"The input type to `HH\\FIXME\\UNSAFE_CAST` should be as specific as possible. Consider using `?string` instead.",
"severity":"error",
"path":"test_quickfix_unsafe_cast.php",
"line":8,
"start":29,
"end":33,
"code":5636,
"bypass_changed_lines":false,
"original":"mixed",
"replacement":"?string",
"start_offset":111,
"width":5
}
],
"version":" Wed Dec 31 16:00:00 1969"
}
2 changes: 1 addition & 1 deletion hphp/hack/test/lint/unsafe_cast_lower_bound2.php.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
File "unsafe_cast_lower_bound2.php", line 4, characters 25-29:
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])
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `string` instead. (Lint[5636])
2 changes: 1 addition & 1 deletion hphp/hack/test/lint/unsafe_cast_lower_bound3.php.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
File "unsafe_cast_lower_bound3.php", line 4, characters 25-29:
HH\FIXME\UNSAFE_CAST input type annotation is too loose, please use a more specific type. (Lint[5636])
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. (Lint[5636])
4 changes: 2 additions & 2 deletions hphp/hack/test/lint/unsafe_cast_lower_bound4.php.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
File "unsafe_cast_lower_bound4.php", line 4, characters 25-29:
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])
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `arraykey` instead. (Lint[5636])
File "unsafe_cast_lower_bound4.php", line 8, characters 25-29:
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])
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `num` instead. (Lint[5636])
2 changes: 1 addition & 1 deletion hphp/hack/test/lint/unsafe_cast_lower_bound5.php.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
File "unsafe_cast_lower_bound5.php", line 4, characters 25-29:
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])
The input type to `HH\FIXME\UNSAFE_CAST` should be as specific as possible. Consider using `?int` instead. (Lint[5636])

0 comments on commit 87903e5

Please sign in to comment.