shuck-linter 0.0.26

Lint rule engine and checker for shell scripts
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
use rustc_hash::{FxHashMap, FxHashSet};
use shuck_ast::{RedirectKind, Span};

use crate::{Checker, ComparablePathKey, FactSpan, Rule, StatementFact, Violation};

pub struct CombineAppends;

impl Violation for CombineAppends {
    fn rule() -> Rule {
        Rule::CombineAppends
    }

    fn message(&self) -> String {
        "multiple commands append to the same file; use one grouped redirect".to_owned()
    }
}

pub fn combine_appends(checker: &mut Checker) {
    let mut bodies: FxHashMap<FactSpan, Vec<StatementFact>> = FxHashMap::default();
    let case_item_body_spans = checker
        .facts()
        .case_items()
        .iter()
        .map(|item| FactSpan::new(item.item().body.span))
        .collect::<FxHashSet<_>>();

    for fact in checker.facts().statement_facts() {
        if case_item_body_spans.contains(&FactSpan::new(fact.body_span())) {
            continue;
        }

        bodies
            .entry(FactSpan::new(fact.body_span()))
            .or_default()
            .push(*fact);
    }

    let spans = bodies
        .into_values()
        .flat_map(|mut statements| append_run_spans_in_body(checker, &mut statements))
        .collect::<Vec<_>>();

    checker.report_all_dedup(spans, || CombineAppends);
}

fn append_run_spans_in_body(checker: &Checker<'_>, statements: &mut [StatementFact]) -> Vec<Span> {
    statements.sort_by_key(|fact| fact.stmt_span().start.offset);

    let mut spans = Vec::new();
    let mut current_key: Option<ComparablePathKey> = None;
    let mut current_run_len = 0usize;
    let mut current_first_span = None;

    for statement in statements.iter() {
        let Some((key, span)) = append_target_for_statement(checker, statement) else {
            if current_run_len >= 3
                && let Some(first_span) = current_first_span.take()
            {
                spans.push(first_span);
            }
            current_key = None;
            current_run_len = 0;
            continue;
        };

        match &current_key {
            Some(existing) if *existing == key => {
                current_run_len += 1;
            }
            _ => {
                if current_run_len >= 3
                    && let Some(first_span) = current_first_span.take()
                {
                    spans.push(first_span);
                }
                current_key = Some(key);
                current_run_len = 1;
                current_first_span = Some(span);
            }
        }
    }

    if current_run_len >= 3
        && let Some(first_span) = current_first_span.take()
    {
        spans.push(first_span);
    }

    spans
}

fn append_target_for_statement(
    checker: &Checker<'_>,
    statement: &StatementFact,
) -> Option<(ComparablePathKey, Span)> {
    let statement_commands = commands_for_statement(checker, statement);
    let mut target: Option<ComparablePathKey> = None;
    let mut anchor_start = None;
    let mut anchor_end = None;

    for command in statement_commands {
        for redirect in command.redirect_facts() {
            if redirect.redirect().kind != RedirectKind::Append {
                continue;
            }

            let analysis = redirect.analysis()?;
            if !analysis.is_file_target() {
                continue;
            }

            let comparable = redirect.comparable_path()?;
            let key = comparable.key().clone();
            if anchor_start.is_none() {
                let command_end = command
                    .body_args()
                    .last()
                    .map(|word| word.span.end)
                    .or_else(|| command.body_word_span().map(|span| span.end))
                    .unwrap_or(command.body_span().end);
                let redirect_end = command
                    .redirect_facts()
                    .iter()
                    .map(|redirect| redirect.redirect().span.end)
                    .max_by_key(|position| position.offset)
                    .unwrap_or(command_end);
                anchor_start = Some(command.body_span().start);
                anchor_end = Some(if redirect_end.offset > command_end.offset {
                    redirect_end
                } else {
                    command_end
                });
            }
            if let Some(current_end) = &mut anchor_end
                && comparable.span().end.offset > current_end.offset
            {
                *current_end = comparable.span().end;
            }

            match &target {
                Some(existing) if *existing == key => {}
                Some(_) => return None,
                None => target = Some(key),
            }
        }
    }

    match (target, anchor_start, anchor_end) {
        (Some(key), Some(anchor_start), Some(anchor_end)) => {
            Some((key, Span::from_positions(anchor_start, anchor_end)))
        }
        (Some(_), _, _) => None,
        (None, _, _) => None,
    }
}

fn commands_for_statement<'checker, 'ast>(
    checker: &'checker Checker<'ast>,
    statement: &StatementFact,
) -> Vec<crate::CommandFactRef<'checker, 'ast>> {
    let command = checker.facts().command(statement.command_id());
    let mut command_ids = FxHashSet::default();
    let mut commands = Vec::new();

    command_ids.insert(statement.command_id());
    commands.push(command);

    if let Some(pipeline) = checker
        .facts()
        .pipelines()
        .iter()
        .find(|pipeline| pipeline.span() == command.span())
    {
        for segment in pipeline.segments() {
            if command_ids.insert(segment.command_id()) {
                commands.push(checker.facts().command(segment.command_id()));
            }
        }
    }

    commands
}

#[cfg(test)]
mod tests {
    use crate::test::test_snippet;
    use crate::{LinterSettings, Rule};

    #[test]
    fn reports_first_command_in_three_command_append_runs() {
        let source = "\
#!/bin/sh
echo one >> out.log
echo two >> out.log
echo three >> out.log
echo first >> semi.log; echo second >> semi.log; echo third >> semi.log
echo alpha >> \"$log\"
echo beta >> \"$log\"
echo gamma >> \"$log\"
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec![
                "echo one >> out.log",
                "echo first >> semi.log",
                "echo alpha >> \"$log\"",
            ]
        );
    }

    #[test]
    fn ignores_two_command_runs_and_already_grouped_redirects() {
        let source = "\
#!/bin/sh
echo one >> out.log
echo two >> out.log
{ echo group one; } >> grouped.log
{ echo group two; } >> grouped.log
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert!(diagnostics.is_empty());
    }

    #[test]
    fn counts_pipeline_statements_and_trims_inline_comment_anchors() {
        let source = "\
#!/bin/sh
echo \"pre_install() {\" >> .INSTALL # comment
cat preinst | grep -v '^#' >> .INSTALL
echo \"}\" >> .INSTALL
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["echo \"pre_install() {\" >> .INSTALL"]
        );
    }

    #[test]
    fn reports_append_runs_inside_loop_bodies() {
        let source = "\
#!/bin/sh
for js in one; do
    echo first >> \"$js\"
    echo second >> \"$js\"
    echo third >> \"$js\"
done
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["echo first >> \"$js\""]
        );
    }

    #[test]
    fn reports_unquoted_parameter_redirect_targets() {
        let source = "\
#!/bin/sh
for js in one; do
    echo first >> $js
    echo second >> $js
    echo third >> $js
done
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["echo first >> $js"]
        );
    }

    #[test]
    fn reports_append_runs_after_non_append_compound_statements() {
        let source = "\
#!/bin/sh
for js in one; do
    if [ -f $js ]; then
        sed -i '/marker/d' $js
    fi
    echo first >> $js
    echo second >> $js
    echo third >> $js
done
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["echo first >> $js"]
        );
    }

    #[test]
    fn counts_pipeline_echo_pipeline_runs() {
        let source = "\
#!/bin/sh
tr ' ' \"$nl\" < \"$tmpdepfile\" \\
  | sed -e 's/^.*\\.o://' -e 's/#.*$//' -e '/^$/ d' \\
  | tr \"$nl\" ' ' >> \"$depfile\"
echo >> \"$depfile\"
tr ' ' \"$nl\" < \"$tmpdepfile\" \\
  | sed -e 's/^.*\\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \\
  >> \"$depfile\"
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["tr \"$nl\" ' ' >> \"$depfile\""]
        );
    }

    #[test]
    fn keeps_trailing_arguments_after_redirect_targets_in_anchor_spans() {
        let source = "\
#!/bin/sh
echo >>$tools '. /tmp/loader'
echo >>$tools ''
echo >>$tools 'cat >script.sh <<EOF'
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["echo >>$tools '. /tmp/loader'"]
        );
    }

    #[test]
    fn matches_gnunet_style_append_runs() {
        let source = "\
#!/bin/sh
for ffprofile in /home/$USER/.mozilla/firefox/*.*/; do
    js=$ffprofile/user.js
    if [ -f $js ]; then
        sed -i '/Preferences for using the GNU Name System/d' $js
        sed -i '/network.proxy.socks/d' $js
        sed -i '/network.proxy.socks_port/d' $js
        sed -i '/network.proxy.socks_remote_dns/d' $js
        sed -i '/network.proxy.type/d' $js
    fi
    echo \"// Preferences for using the GNU Name System\" >> $js
    echo \"user_pref(\\\"network.proxy.socks\\\", \\\"localhost\\\");\" >> $js
    echo \"user_pref(\\\"network.proxy.socks_port\\\", $PORT);\" >> $js
    echo \"user_pref(\\\"network.proxy.socks_remote_dns\\\", true);\" >> $js
    echo \"user_pref(\\\"network.proxy.type\\\", 1);\" >> $js
done
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["echo \"// Preferences for using the GNU Name System\" >> $js"]
        );
    }

    #[test]
    fn matches_depcomp_style_append_runs() {
        let source = "\
#!/bin/sh
tr ' ' \"$nl\" < \"$tmpdepfile\" \\
  | sed -e 's/^.*\\.o://' -e 's/#.*$//' -e '/^$/ d' \\
  | tr \"$nl\" ' ' >> \"$depfile\"
echo >> \"$depfile\"
# The second pass generates a dummy entry for each header file.
tr ' ' \"$nl\" < \"$tmpdepfile\" \\
  | sed -e 's/^.*\\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \\
  >> \"$depfile\"
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["tr \"$nl\" ' ' >> \"$depfile\""]
        );
    }

    #[test]
    fn anchors_pipeline_runs_at_the_redirecting_segment() {
        let source = "\
#!/bin/sh
printf '%s\\n' one \\
  | sed 's/o/o/' >> out.log
echo two >> out.log
echo three >> out.log
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["sed 's/o/o/' >> out.log"]
        );
    }

    #[test]
    fn ignores_top_level_append_runs_inside_case_arms() {
        let source = "\
#!/bin/sh
case \"$kind\" in
  kernel)
    echo one >> out.log
    echo two >> out.log
    echo three >> out.log
    ;;
esac
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert!(diagnostics.is_empty());
    }

    #[test]
    fn reports_nested_append_runs_inside_case_arms() {
        let source = "\
#!/bin/sh
case \"$kind\" in
  kernel)
    if test -f \"$tmpdepfile\"; then
      tr ' ' \"$nl\" < \"$tmpdepfile\" \\
        | sed -e 's/^.*\\.o://' -e 's/#.*$//' -e '/^$/ d' \\
        | tr \"$nl\" ' ' >> \"$depfile\"
      echo >> \"$depfile\"
      tr ' ' \"$nl\" < \"$tmpdepfile\" \\
        | sed -e 's/^.*\\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \\
        >> \"$depfile\"
    fi
    ;;
esac
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["tr \"$nl\" ' ' >> \"$depfile\""]
        );
    }

    #[test]
    fn keeps_trailing_heredoc_redirects_in_anchor_spans() {
        let source = "\
#!/bin/sh
cat >>confdefs.h <<_ACEOF
#define PACKAGE_NAME \"$PACKAGE_NAME\"
_ACEOF
printf '%s\\n' done >>confdefs.h
printf '%s\\n' again >>confdefs.h
";
        let diagnostics = test_snippet(source, &LinterSettings::for_rule(Rule::CombineAppends));

        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["cat >>confdefs.h <<_ACEOF"]
        );
    }
}