Skip to content

Commit

Permalink
[autofix] Add autofix for unnecessary copy on field init exp
Browse files Browse the repository at this point in the history
Summary:
```
C(T f): f_(f) {}
=================
C(T f): f_(std::move(f)) {}
```

This is one of the frequent unnecessary copy patterns that is
straightforward to fix.  This diff generates autofix when

- the procedure is a constructor and
- a field is initialized by a parameter.

Reviewed By: ngorogiannis

Differential Revision: D63505474

fbshipit-source-id: da0b162d4bc6e75ec9a6509707bee91a8f8868b9
  • Loading branch information
skcho authored and facebook-github-bot committed Sep 27, 2024
1 parent 995d569 commit 0577b64
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
21 changes: 21 additions & 0 deletions infer/src/pulse/PulseDiagnostic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,27 @@ let get_message_and_suggestion diagnostic =
, Some (get_suggestion_msg source_opt) ) )


let get_autofix pdesc diagnostic =
match diagnostic with
| UnnecessaryCopy {copied_into; source_opt; copied_location= None} -> (
let is_formal pvar =
let pvar_name = Pvar.get_name pvar in
List.exists (Procdesc.get_formals pdesc) ~f:(fun (formal, _, _) ->
Mangled.equal pvar_name formal )
in
match (copied_into, source_opt) with
| IntoField {field}, Some (DecompilerExpr.PVar pvar, [Dereference])
when Procname.is_constructor (Procdesc.get_proc_name pdesc) && is_formal pvar ->
let param = Pvar.to_string pvar in
Some
{ Jsonbug_t.original= F.asprintf "%a(%s)" Fieldname.pp field param
; replacement= F.asprintf "%a(std::move(%s))" Fieldname.pp field param }
| _ ->
None )
| _ ->
None


let add_errlog_header ~nesting ~title location errlog =
let tags = [] in
Errlog.make_trace_element nesting location title tags :: errlog
Expand Down
2 changes: 2 additions & 0 deletions infer/src/pulse/PulseDiagnostic.mli
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ val aborts_execution : t -> bool

val get_message_and_suggestion : t -> string * string option

val get_autofix : Procdesc.t -> t -> Jsonbug_t.autofix option

val get_location : t -> Location.t

val get_location_instantiated : t -> Location.t option
Expand Down
3 changes: 2 additions & 1 deletion infer/src/pulse/PulseReport.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ let report {InterproceduralAnalysis.tenv; proc_desc; err_log} ~is_suppressed ~la
in
let issue_type = get_issue_type tenv ~latent diagnostic proc_desc in
let message, suggestion = get_message_and_suggestion diagnostic in
let autofix = get_autofix proc_desc diagnostic in
L.d_printfln ~color:Red "Reporting issue: %a: %s" IssueType.pp issue_type message ;
Reporting.log_issue proc_desc err_log ~loc:(get_location diagnostic) ?loc_instantiated
~ltr:(extra_trace @ get_trace diagnostic)
~extras ?suggestion Pulse issue_type message
~extras ?autofix ?suggestion Pulse issue_type message


let report_latent_issue analysis_data latent_issue ~is_suppressed =
Expand Down

0 comments on commit 0577b64

Please sign in to comment.