perl-lsp-code-actions 0.12.2

LSP code actions provider for Perl
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! Quick fixes for diagnostic issues
//!
//! Provides automated fixes for common Perl issues driven by diagnostic codes.

use crate::types::{CodeAction, CodeActionEdit, CodeActionKind, QuickFixDiagnostic};
use perl_ast_utils::{find_declaration_position, get_indent_at};
use perl_diagnostics_codes::DiagnosticCode;
use perl_lsp_rename::TextEdit;
use perl_parser_core::SourceLocation;

/// Fix undefined variable by declaring it
pub fn fix_undefined_variable(source: &str, diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    // Extract variable name from diagnostic message
    if let Some(var_name) = diagnostic.message.split('\'').nth(1) {
        // Find the best place to insert declaration
        let insert_pos = find_declaration_position(source, diagnostic.range.0);

        // Add 'my' declaration
        actions.push(CodeAction {
            title: format!("Declare '{}' with 'my'", var_name),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::UndefinedVariable.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: insert_pos, end: insert_pos },
                    new_text: format!("my {};\n", var_name),
                }],
            },
            is_preferred: true,
        });

        // Add 'our' declaration
        actions.push(CodeAction {
            title: format!("Declare '{}' with 'our'", var_name),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::UndefinedVariable.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: insert_pos, end: insert_pos },
                    new_text: format!("our {};\n", var_name),
                }],
            },
            is_preferred: false,
        });
    }

    actions
}

/// Fix unused variable by removing it
pub fn fix_unused_variable(source: &str, diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    // Find the declaration line
    let line_start = source[..diagnostic.range.0].rfind('\n').map(|p| p + 1).unwrap_or(0);
    let line_end = source[diagnostic.range.1..]
        .find('\n')
        .map(|p| diagnostic.range.1 + p)
        .unwrap_or(source.len());

    actions.push(CodeAction {
        title: "Remove unused variable".to_string(),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::UnusedVariable.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: line_start, end: line_end + 1 },
                new_text: String::new(),
            }],
        },
        is_preferred: true,
    });

    // Add underscore prefix to mark as intentionally unused
    if let Some(var_name) = diagnostic.message.split('\'').nth(1) {
        actions.push(CodeAction {
            title: format!("Rename to '_{}'", var_name),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::UnusedVariable.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.1 },
                    new_text: format!("_{}", var_name),
                }],
            },
            is_preferred: false,
        });
    }

    actions
}

/// Fix assignment in condition
pub fn fix_assignment_in_condition(
    source: &str,
    diagnostic: &QuickFixDiagnostic,
) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    // Change = to ==
    let assignment_pos =
        source[diagnostic.range.0..diagnostic.range.1].find('=').map(|p| diagnostic.range.0 + p);

    if let Some(pos) = assignment_pos {
        actions.push(CodeAction {
            title: "Change to comparison (==)".to_string(),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::AssignmentInCondition.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: pos, end: pos + 1 },
                    new_text: "==".to_string(),
                }],
            },
            is_preferred: true,
        });

        // Wrap in parentheses to make intention clear
        actions.push(CodeAction {
            title: "Keep assignment (add parentheses)".to_string(),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::AssignmentInCondition.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![
                    TextEdit {
                        location: SourceLocation {
                            start: diagnostic.range.0,
                            end: diagnostic.range.0,
                        },
                        new_text: "(".to_string(),
                    },
                    TextEdit {
                        location: SourceLocation {
                            start: diagnostic.range.1,
                            end: diagnostic.range.1,
                        },
                        new_text: ")".to_string(),
                    },
                ],
            },
            is_preferred: false,
        });
    }

    actions
}

/// Add 'use strict' pragma
pub fn add_use_strict() -> Vec<CodeAction> {
    vec![CodeAction {
        title: "Add 'use strict'".to_string(),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::MissingStrict.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: 0, end: 0 },
                new_text: "use strict;\n".to_string(),
            }],
        },
        is_preferred: true,
    }]
}

/// Add 'use warnings' pragma
pub fn add_use_warnings() -> Vec<CodeAction> {
    vec![CodeAction {
        title: "Add 'use warnings'".to_string(),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::MissingWarnings.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: 0, end: 0 },
                new_text: "use warnings;\n".to_string(),
            }],
        },
        is_preferred: true,
    }]
}

/// Fix deprecated 'defined @array' or 'defined %hash'
pub fn fix_deprecated_defined(source: &str, diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    // Extract the array/hash from the diagnostic
    if let Some(start) = source[diagnostic.range.0..diagnostic.range.1].find("defined") {
        let defined_start = diagnostic.range.0 + start;
        let arg_start = defined_start + 7; // "defined".len()

        // Find the argument
        let arg_text = &source[arg_start..diagnostic.range.1].trim();

        actions.push(CodeAction {
            title: format!("Replace with 'if ({})'", arg_text),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::DeprecatedDefined.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: defined_start, end: diagnostic.range.1 },
                    new_text: arg_text.to_string(),
                }],
            },
            is_preferred: true,
        });
    }

    actions
}

/// Fix numeric comparison with undef
pub fn fix_numeric_undef(source: &str, diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    // Add defined check
    actions.push(CodeAction {
        title: "Add defined check".to_string(),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::NumericComparisonWithUndef.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![
                TextEdit {
                    location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.0 },
                    new_text: "defined(".to_string(),
                },
                TextEdit {
                    location: SourceLocation { start: diagnostic.range.1, end: diagnostic.range.1 },
                    new_text: ")".to_string(),
                },
            ],
        },
        is_preferred: true,
    });

    // Use // operator
    if source[diagnostic.range.0..diagnostic.range.1].contains("==") {
        actions.push(CodeAction {
            title: "Use defined-or operator (//)".to_string(),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::NumericComparisonWithUndef.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.1 },
                    new_text: "// 0".to_string(), // Default to 0
                }],
            },
            is_preferred: false,
        });
    }

    actions
}

/// Fix unquoted bareword by quoting or declaring as filehandle
///
/// Provides three options for fixing bareword issues under strict mode:
/// 1. Quote with single quotes - wraps bareword in single quotes
/// 2. Quote with double quotes - wraps bareword in double quotes
/// 3. Declare as filehandle - for uppercase barewords, adds filehandle declaration
pub fn fix_bareword(source: &str, diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    // Extract bareword text from the source at the diagnostic range
    let bareword = &source[diagnostic.range.0..diagnostic.range.1];

    // Check if bareword is all uppercase (filehandle convention)
    let is_uppercase = bareword.chars().all(|c| c.is_ascii_uppercase() || c == '_');

    // Action 1: Quote with single quotes
    actions.push(CodeAction {
        title: format!("Quote '{}' with single quotes", bareword),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::UnquotedBareword.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.1 },
                new_text: format!("'{}'", bareword),
            }],
        },
        is_preferred: true,
    });

    // Action 2: Quote with double quotes
    actions.push(CodeAction {
        title: format!("Quote '{}' with double quotes", bareword),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::UnquotedBareword.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.1 },
                new_text: format!("\"{}\"", bareword),
            }],
        },
        is_preferred: false,
    });

    // Action 3: Declare as filehandle (only for uppercase barewords)
    if is_uppercase {
        // Find the best position to insert a filehandle declaration
        let insert_pos = find_declaration_position(source, diagnostic.range.0);
        let indent = get_indent_at(source, insert_pos);

        actions.push(CodeAction {
            title: format!("Declare '{}' as filehandle", bareword),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::UnquotedBareword.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: insert_pos, end: insert_pos },
                    new_text: format!("{}open my ${};\n", indent, bareword),
                }],
            },
            is_preferred: false,
        });
    }

    actions
}

/// Fix parse errors with automated corrections
pub fn fix_parse_error(
    source: &str,
    diagnostic: &QuickFixDiagnostic,
    code: &str,
) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    match code {
        "parse-error-missingsemicolon" => {
            // Add semicolon at the end
            let line_end = source[diagnostic.range.0..]
                .find('\n')
                .map(|p| diagnostic.range.0 + p)
                .unwrap_or(source.len());

            // Find the actual end of the statement (before any trailing whitespace)
            let mut end_pos = line_end;
            while end_pos > diagnostic.range.0
                && source.as_bytes()[end_pos - 1].is_ascii_whitespace()
            {
                end_pos -= 1;
            }

            actions.push(CodeAction {
                title: "Add missing semicolon".to_string(),
                kind: CodeActionKind::QuickFix,
                diagnostics: vec![code.to_string()],
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation { start: end_pos, end: end_pos },
                        new_text: ";".to_string(),
                    }],
                },
                is_preferred: true,
            });
        }
        "PL001" | "PL002"
            if diagnostic.message.to_ascii_lowercase().contains("missing semicolon") =>
        {
            // PL001/PL002 are general parse error codes. When the message indicates a missing
            // semicolon, apply the same fix — but skip heredoc contexts where insertion is wrong.
            let at_heredoc = source[diagnostic.range.0..].get(..2).is_some_and(|s| s == "<<");
            if !at_heredoc {
                let line_end = source[diagnostic.range.0..]
                    .find('\n')
                    .map(|p| diagnostic.range.0 + p)
                    .unwrap_or(source.len());

                // Insert before trailing whitespace
                let mut end_pos = line_end;
                while end_pos > diagnostic.range.0
                    && source.as_bytes()[end_pos - 1].is_ascii_whitespace()
                {
                    end_pos -= 1;
                }

                actions.push(CodeAction {
                    title: "Add missing semicolon".to_string(),
                    kind: CodeActionKind::QuickFix,
                    diagnostics: vec![code.to_string()],
                    edit: CodeActionEdit {
                        changes: vec![TextEdit {
                            location: SourceLocation { start: end_pos, end: end_pos },
                            new_text: ";".to_string(),
                        }],
                    },
                    is_preferred: true,
                });
            }
        }
        "parse-error-unclosedstring" => {
            // Add closing quote
            actions.push(CodeAction {
                title: "Add closing quote".to_string(),
                kind: CodeActionKind::QuickFix,
                diagnostics: vec![code.to_string()],
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation {
                            start: diagnostic.range.1,
                            end: diagnostic.range.1,
                        },
                        new_text: "\"".to_string(),
                    }],
                },
                is_preferred: true,
            });
        }
        "parse-error-unclosedparenthesis" => {
            actions.push(CodeAction {
                title: "Add closing parenthesis".to_string(),
                kind: CodeActionKind::QuickFix,
                diagnostics: vec![code.to_string()],
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation {
                            start: diagnostic.range.1,
                            end: diagnostic.range.1,
                        },
                        new_text: ")".to_string(),
                    }],
                },
                is_preferred: true,
            });
        }
        "parse-error-unclosedbracket" => {
            actions.push(CodeAction {
                title: "Add closing bracket".to_string(),
                kind: CodeActionKind::QuickFix,
                diagnostics: vec![code.to_string()],
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation {
                            start: diagnostic.range.1,
                            end: diagnostic.range.1,
                        },
                        new_text: "]".to_string(),
                    }],
                },
                is_preferred: true,
            });
        }
        "parse-error-unclosedbrace" | "parse-error-unclosedblock" => {
            actions.push(CodeAction {
                title: "Add closing brace".to_string(),
                kind: CodeActionKind::QuickFix,
                diagnostics: vec![code.to_string()],
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation {
                            start: diagnostic.range.1,
                            end: diagnostic.range.1,
                        },
                        new_text: "}".to_string(),
                    }],
                },
                is_preferred: true,
            });
        }
        _ => {}
    }

    actions
}

/// Fix unused parameter by adding underscore prefix
pub fn fix_unused_parameter(diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    if let Some(param_name) = diagnostic.message.split('\'').nth(1) {
        // Add underscore prefix
        actions.push(CodeAction {
            title: format!("Rename to '_{}'", param_name),
            kind: CodeActionKind::QuickFix,
            diagnostics: vec![DiagnosticCode::UnusedParameter.as_str().to_string()],
            edit: CodeActionEdit {
                changes: vec![TextEdit {
                    location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.1 },
                    new_text: format!("_{}", param_name),
                }],
            },
            is_preferred: true,
        });
    }

    actions
}

/// Suggest portable shebang line
///
/// Detects hardcoded perl paths in shebang lines (e.g., `#!/usr/bin/perl`,
/// `#!/usr/local/bin/perl`) and suggests replacing with `#!/usr/bin/env perl`
/// for better portability across systems.
///
/// Only triggers on the first line of the file when it starts with `#!` and
/// contains a path to perl that is not already using `env`.
pub fn fix_hardcoded_shebang(source: &str) -> Vec<CodeAction> {
    let first_line = match source.lines().next() {
        Some(line) => line,
        None => return Vec::new(),
    };

    // Must be a shebang line
    if !first_line.starts_with("#!") {
        return Vec::new();
    }

    // Already portable
    if first_line.contains("/env ") || first_line.contains("/env\t") {
        return Vec::new();
    }

    // Must reference perl
    if !first_line.contains("perl") {
        return Vec::new();
    }

    // Extract any flags after the perl path (e.g., -w, -T)
    let flags = extract_shebang_flags(first_line);
    let new_shebang = if flags.is_empty() {
        "#!/usr/bin/env perl".to_string()
    } else {
        format!("#!/usr/bin/env perl {}", flags)
    };

    vec![CodeAction {
        title: "Use portable shebang (#!/usr/bin/env perl)".to_string(),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec!["hardcoded-shebang".to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: 0, end: first_line.len() },
                new_text: new_shebang,
            }],
        },
        is_preferred: true,
    }]
}

/// Extract flags from a shebang line (e.g., `-w` from `#!/usr/bin/perl -w`)
fn extract_shebang_flags(shebang_line: &str) -> String {
    // Find "perl" in the line, then grab everything after it
    if let Some(perl_pos) = shebang_line.find("perl") {
        let after_perl = &shebang_line[perl_pos + 4..];
        let trimmed = after_perl.trim();
        if trimmed.is_empty() { String::new() } else { trimmed.to_string() }
    } else {
        String::new()
    }
}

/// Fix variable shadowing by suggesting rename
pub fn fix_variable_shadowing(diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    if let Some(var_name) = diagnostic.message.split('\'').nth(1) {
        // Remove sigil for the base name
        let base_name =
            var_name.trim_start_matches('$').trim_start_matches('@').trim_start_matches('%');

        // Suggest alternative names
        let suggestions = vec![
            format!("{}_inner", base_name),
            format!("{}_local", base_name),
            format!("my_{}", base_name),
        ];

        for suggestion in suggestions {
            let new_name = if var_name.starts_with('$') {
                format!("${}", suggestion)
            } else if var_name.starts_with('@') {
                format!("@{}", suggestion)
            } else if var_name.starts_with('%') {
                format!("%{}", suggestion)
            } else {
                suggestion.clone()
            };

            actions.push(CodeAction {
                title: format!("Rename to '{}'", new_name),
                kind: CodeActionKind::QuickFix,
                diagnostics: vec![DiagnosticCode::VariableShadowing.as_str().to_string()],
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation {
                            start: diagnostic.range.0,
                            end: diagnostic.range.1,
                        },
                        new_text: new_name,
                    }],
                },
                is_preferred: false,
            });
        }
    }

    actions
}

/// Fix bareword filehandle by replacing with lexical filehandle
///
/// Bareword filehandles (e.g., `open FILE, ...`) are a common Perl anti-pattern.
/// This fix suggests replacing the bareword with a lexical variable (`my $fh`).
pub fn fix_bareword_filehandle(diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    // Extract filehandle name from message, e.g. "Bareword filehandle 'FILE'"
    let fh_name = diagnostic.message.split('\'').nth(1).unwrap_or("FH");
    // Derive a lowercase lexical name: FILE -> $file_fh, LOGFILE -> $logfile_fh
    let lexical_name = format!("${}_fh", fh_name.to_lowercase());

    vec![CodeAction {
        title: format!("Replace bareword filehandle '{}' with lexical '{}'", fh_name, lexical_name),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::BarewordFilehandle.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.1 },
                new_text: format!("my {}", lexical_name),
            }],
        },
        is_preferred: true,
    }]
}

/// Suggest upgrading two-argument open() to three-argument form
///
/// Two-argument `open($fh, $filename)` is unsafe because `$filename` can
/// contain shell metacharacters. The three-argument form separates the mode
/// from the filename, e.g. `open(my $fh, '<', $filename)`.
pub fn fix_two_arg_open(diagnostic: &QuickFixDiagnostic) -> Vec<CodeAction> {
    vec![CodeAction {
        title: "Convert to three-argument open() for safety".to_string(),
        kind: CodeActionKind::QuickFix,
        diagnostics: vec![DiagnosticCode::TwoArgOpen.as_str().to_string()],
        edit: CodeActionEdit {
            changes: vec![TextEdit {
                location: SourceLocation { start: diagnostic.range.0, end: diagnostic.range.1 },
                new_text: "open(my $fh, '<', $filename)".to_string(),
            }],
        },
        is_preferred: true,
    }]
}