Files
taylor.fish 663d809194 runtime(rust): Fix Rust indentation when string contains "if"
indent/rust.vim behaves incorrectly when a string literal contains the
substring "if".

For example, in this code:

    let x = "
                motif
    ";
    struct X {
                }

indent/rust.vim thinks that the closing "}" should line up with "motif".

This patch fixes the issue by checking whether the "if" is in a string
literal or comment before considering it to be a match for a subsequent
brace (and also by requiring it to start on a word boundary).

Add an indent test to ensure this does not regress.

closes: #19265

Signed-off-by: taylor.fish <contact@taylor.fish>
Signed-off-by: Christian Brabandt <cb@256bit.org>
2026-01-28 22:07:16 +00:00

51 lines
1.2 KiB
Rust

// vim: set ft=rust ts=8 sw=4 sts=4 et :
// START_INDENT
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
fn main() {
// Create a path to the desired file
let path = Path::new("hello.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let mut file = match File::open(&path) {
Err(why) => panic!("couldn't open {}: {}", display, why),
Ok(file) => file,
};
// Start doing nothing forever
loop {
let arr1 = [[u8; 4]; 2] = [
[0; 4],
[1, 3, 5, 9],
];
}
// Plan for a future that will never come
let arr2 = [[u8; 4]; 2] = [
[1; 4],
[2, 4, 6, 8],
];
let arr2_ref = &arr2;
// Read the file contents into a string, returns `io::Result<usize>`
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("couldn't read {}: {}", display, why),
Ok(_) => print!("{} contains:\n{}", display, s),
}
// file goes out of scope, and the "hello.txt" file gets closed
}
// END_INDENT
// START_INDENT
let x = "
if fn motif
";
struct X {
}
// END_INDENT