arity 0.27.0

A language server, formatter, and linter for R
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
//! `sprintf`: validate literal base `sprintf()` and `gettextf()` formats. The format
//! scanner models escaped percent signs, numbered fields, and argument-taking
//! `*` widths and precisions. It reports only definite arity errors across all
//! literal formats, because `sprintf()` recycles format vectors and values.

use std::collections::HashSet;
use std::ops::Range;

use rowan::ast::AstNode as _;

use crate::ast::{BinaryExpr, CallExpr};
use crate::linter::diagnostic::{Diagnostic, Fix, ViolationData};
use crate::linter::rules::matchers;
use crate::linter::rules::{Example, Rule, RuleContext};
use crate::syntax::{SyntaxElement, SyntaxKind, SyntaxToken};

pub struct Sprintf;

impl Rule for Sprintf {
    fn id(&self) -> &'static str {
        "sprintf"
    }

    fn description(&self) -> &'static str {
        "Validate literal formats passed to base `sprintf()` and `gettextf()`: flag invalid conversions, definitely missing or excess arguments, and platform-dependent zero padding of strings (`0` plus a width on `%s`). Use explicit string padding or an appropriate numeric conversion instead; neither is a general safe rewrite. A literal-only `sprintf()` call has a safe autofix. Literal-only `gettextf()` calls retain translation."
    }

    fn examples(&self) -> &'static [Example] {
        &[
            Example {
                caption: "A literal format with no fields needs no `sprintf()` call:",
                source: "label <- sprintf(\"ready: 100%%\")\n",
            },
            Example {
                caption: "Zero padding of strings depends on the platform:",
                source: "label <- sprintf(f = \"%05s\", \"x\")\n",
            },
            Example {
                caption: "The same portability check applies to translated formats:",
                source: "label <- gettextf(\"%0*s\", 5, \"x\", domain = NA)\n",
            },
        ]
    }

    fn interests(&self) -> &'static [SyntaxKind] {
        &[SyntaxKind::CALL_EXPR]
    }

    fn check(&self, el: &SyntaxElement, ctx: &RuleContext<'_>, sink: &mut Vec<Diagnostic>) {
        let Some(call) = el.as_node().cloned().and_then(CallExpr::cast) else {
            return;
        };
        let Some(name) = matchers::callee_name(&call) else {
            return;
        };
        if !matches!(name.as_str(), "sprintf" | "gettextf") || !is_base_call(&call, ctx) {
            return;
        }

        let args = matchers::args(&call);
        let Some(fmt_index) = format_arg_index(&args) else {
            return;
        };
        let Some(fmt) = args[fmt_index].value.as_ref() else {
            return;
        };
        let Some(formats) = literal_formats(fmt, ctx) else {
            return;
        };
        let value_count = args
            .iter()
            .enumerate()
            .filter(|(i, arg)| {
                *i != fmt_index
                    && arg.value.is_some()
                    && !(name == "gettextf"
                        && matches!(arg.name.as_deref(), Some("domain" | "trim")))
            })
            .count();

        let mut used = HashSet::new();
        let mut fields = 0;
        let mut encoded_percent = false;
        for token in &formats {
            let Some((_, inner)) = matchers::string_literal(token) else {
                return;
            };
            let decoded = decode_format(inner);
            encoded_percent |= decoded.encoded_percent;
            match scan_format(&decoded.bytes) {
                Ok(analysis) => {
                    fields += analysis.fields;
                    used.extend(analysis.used);
                    for field in analysis.zero_padded_strings {
                        let start = decoded.source_spans[field.start].start;
                        let end = decoded.source_spans[field.end - 1].end;
                        let offset = token.text_range().start() + rowan::TextSize::from(1);
                        push_finding(
                            sink,
                            rowan::TextRange::new(
                                offset + rowan::TextSize::from(start as u32),
                                offset + rowan::TextSize::from(end as u32),
                            ),
                            format!(
                                "`{name}()` string conversion `{}` has platform-dependent zero padding",
                                String::from_utf8_lossy(&decoded.bytes[field])
                            ),
                            "Use explicit string padding or an appropriate numeric conversion.",
                            None,
                        );
                    }
                }
                Err(conversion) => {
                    push_finding(
                        sink,
                        token.text_range(),
                        format!("invalid `{name}()` conversion `{conversion}`"),
                        "Use a conversion supported by base `sprintf()`.",
                        None,
                    );
                    return;
                }
            }
        }

        // Forwarded dots may supply any number of values. The format itself
        // still establishes portability problems, but cannot prove an arity error.
        if args.iter().any(|arg| {
            arg.value
                .as_ref()
                .and_then(SyntaxElement::as_token)
                .is_some_and(|token| token.kind() == SyntaxKind::IDENT && token.text() == "...")
        }) {
            return;
        }

        if let Some(missing) = used.iter().copied().filter(|n| *n > value_count).min() {
            push_finding(
                sink,
                fmt.text_range(),
                format!("`{name}()` format references missing argument {missing}"),
                "Supply every argument referenced by the format.",
                None,
            );
        } else if value_count > used.len() {
            push_finding(
                sink,
                call.syntax().text_range(),
                format!(
                    "`{name}()` has {} excess argument(s)",
                    value_count - used.len()
                ),
                "Remove arguments that the format does not use.",
                None,
            );
        } else if name == "sprintf" && fields == 0 && value_count == 0 && formats.len() == 1 {
            let token = &formats[0];
            let Some((quote, inner)) = matchers::string_literal(token) else {
                return;
            };
            let range = call.syntax().text_range();
            let fix = if encoded_percent {
                None
            } else {
                let replacement = format!("{quote}{}{quote}", inner.replace("%%", "%"));
                Some(Fix::safe(
                    range.start().into(),
                    range.end().into(),
                    replacement,
                    "Replace `sprintf()` with the literal",
                ))
            };
            push_finding(
                sink,
                range,
                "`sprintf()` is pointless because the format has no fields".into(),
                "Use the string literal directly.",
                fix,
            );
        }
    }
}

fn is_base_call(call: &CallExpr, ctx: &RuleContext<'_>) -> bool {
    ctx.resolves_to_base(call)
        || call
            .base()
            .and_then(SyntaxElement::into_node)
            .and_then(BinaryExpr::cast)
            .and_then(|binary| binary.namespace_access())
            .is_some_and(|access| access.package == "base")
}

fn format_arg_index(args: &[matchers::ArgMatch]) -> Option<usize> {
    // Exact matching precedes partial matching. Once `fmt` has an exact match,
    // `f` and `fm` are values in `...`, but multiple matches to `fmt` are errors.
    for names in [&["fmt"][..], &["f", "fm"][..]] {
        let mut matches = args.iter().enumerate().filter(|(_, arg)| {
            arg.name
                .as_deref()
                .is_some_and(|name| names.contains(&name))
        });
        if let Some((index, _)) = matches.next() {
            return matches.next().is_none().then_some(index);
        }
    }
    args.iter().position(|arg| arg.name.is_none())
}

fn literal_formats(value: &SyntaxElement, ctx: &RuleContext<'_>) -> Option<Vec<SyntaxToken>> {
    if let Some(token) = value.as_token() {
        return matchers::string_literal(token).map(|_| vec![token.clone()]);
    }
    let call = value.as_node().cloned().and_then(CallExpr::cast)?;
    if matchers::callee_name(&call).as_deref() != Some("c") || !is_base_call(&call, ctx) {
        return None;
    }
    matchers::args(&call)
        .into_iter()
        .map(|arg| {
            let token = arg.value?.into_token()?;
            matchers::string_literal(&token)?;
            Some(token)
        })
        .collect()
}

struct FormatAnalysis {
    used: HashSet<usize>,
    fields: usize,
    zero_padded_strings: Vec<Range<usize>>,
}

fn scan_format(bytes: &[u8]) -> Result<FormatAnalysis, String> {
    let mut i = 0;
    let mut next = 1;
    let mut used = HashSet::new();
    let mut fields = 0;
    let mut zero_padded_strings = Vec::new();
    while i < bytes.len() {
        if bytes[i] != b'%' {
            i += 1;
            continue;
        }
        let field_start = i;
        i += 1;
        if bytes.get(i) == Some(&b'%') {
            i += 1;
            continue;
        }
        fields += 1;
        let field_arg = numbered(bytes, &mut i);
        let mut zero_pad = false;
        while bytes.get(i).is_some_and(|b| b"-+ 0#".contains(b)) {
            zero_pad |= bytes[i] == b'0';
            i += 1;
        }
        let has_width = bytes.get(i) == Some(&b'*') || bytes.get(i).is_some_and(u8::is_ascii_digit);
        if bytes.get(i) == Some(&b'*') {
            i += 1;
            consume(numbered(bytes, &mut i), &mut next, &mut used);
        } else {
            while bytes.get(i).is_some_and(u8::is_ascii_digit) {
                i += 1;
            }
        }
        if bytes.get(i) == Some(&b'.') {
            i += 1;
            if bytes.get(i) == Some(&b'*') {
                i += 1;
                consume(numbered(bytes, &mut i), &mut next, &mut used);
            } else {
                while bytes.get(i).is_some_and(u8::is_ascii_digit) {
                    i += 1;
                }
            }
        }
        let Some(&conversion) = bytes.get(i) else {
            return Err("%<end>".into());
        };
        if !b"diouxXfFeEgGaAcs".contains(&conversion) {
            return Err(format!("%{}", char::from(conversion)));
        }
        i += 1;
        if conversion == b's' && zero_pad && has_width {
            zero_padded_strings.push(field_start..i);
        }
        consume(field_arg, &mut next, &mut used);
    }
    Ok(FormatAnalysis {
        used,
        fields,
        zero_padded_strings,
    })
}

struct DecodedFormat {
    bytes: Vec<u8>,
    source_spans: Vec<Range<usize>>,
    encoded_percent: bool,
}

impl DecodedFormat {
    fn push(&mut self, byte: u8, span: Range<usize>) {
        self.bytes.push(byte);
        self.encoded_percent |= byte == b'%' && span.len() > 1;
        self.source_spans.push(span);
    }
}

/// Keep each decoded byte's source span so escaped flags and fields can be
/// diagnosed without pointing into the decoded rather than original string.
fn decode_format(raw: &str) -> DecodedFormat {
    let bytes = raw.as_bytes();
    let mut out = DecodedFormat {
        bytes: Vec::with_capacity(raw.len()),
        source_spans: Vec::with_capacity(raw.len()),
        encoded_percent: false,
    };
    let mut i = 0;
    while i < bytes.len() {
        let escape_start = i;
        if bytes[i] != b'\\' {
            out.push(bytes[i], i..i + 1);
            i += 1;
            continue;
        }
        i += 1;
        let Some(&escaped) = bytes.get(i) else { break };
        if escaped == b'x' {
            i += 1;
            let start = i;
            while i < bytes.len() && i - start < 2 && bytes[i].is_ascii_hexdigit() {
                i += 1;
            }
            if let Ok(value) = u8::from_str_radix(&raw[start..i], 16) {
                out.push(value, escape_start..i);
            }
        } else if (b'0'..=b'7').contains(&escaped) {
            let start = i;
            while i < bytes.len() && i - start < 3 && (b'0'..=b'7').contains(&bytes[i]) {
                i += 1;
            }
            if let Ok(value) = u8::from_str_radix(&raw[start..i], 8) {
                out.push(value, escape_start..i);
            }
        } else if matches!(escaped, b'u' | b'U') {
            let digits = if escaped == b'u' { 4 } else { 8 };
            i += 1;
            let braced = bytes.get(i) == Some(&b'{');
            if braced {
                i += 1;
            }
            let start = i;
            while i < bytes.len() && i - start < digits && bytes[i].is_ascii_hexdigit() {
                i += 1;
            }
            let value = u32::from_str_radix(&raw[start..i], 16).ok();
            if braced && bytes.get(i) == Some(&b'}') {
                i += 1;
            }
            if let Some(value) = value.and_then(char::from_u32) {
                let mut encoded = [0; 4];
                for byte in value.encode_utf8(&mut encoded).bytes() {
                    out.push(byte, escape_start..i);
                }
            }
        } else {
            i += 1;
            let decoded = match escaped {
                b'a' => 7,
                b'b' => 8,
                b'f' => 12,
                b'n' => b'\n',
                b'r' => b'\r',
                b't' => b'\t',
                b'v' => 11,
                _ => escaped,
            };
            out.push(decoded, escape_start..i);
        }
    }
    out
}

fn numbered(bytes: &[u8], i: &mut usize) -> Option<usize> {
    let start = *i;
    while bytes.get(*i).is_some_and(u8::is_ascii_digit) {
        *i += 1;
    }
    if *i > start && bytes.get(*i) == Some(&b'$') {
        let number = std::str::from_utf8(&bytes[start..*i]).ok()?.parse().ok();
        *i += 1;
        number
    } else {
        *i = start;
        None
    }
}

fn consume(explicit: Option<usize>, next: &mut usize, used: &mut HashSet<usize>) {
    let index = explicit.unwrap_or_else(|| {
        let index = *next;
        *next += 1;
        index
    });
    used.insert(index);
}

fn push_finding(
    sink: &mut Vec<Diagnostic>,
    range: rowan::TextRange,
    body: String,
    suggestion: &str,
    fix: Option<Fix>,
) {
    sink.push(Diagnostic {
        rule: "sprintf",
        severity: Default::default(),
        path: Default::default(),
        range,
        message: ViolationData::new("sprintf", body).with_suggestion(suggestion),
        fix,
    });
}