Skip to content

Use more raw string literals for clarity + minor cleanup #1907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::process::Command;

fn main() {
let version = Command::new(if cfg!(windows) { "git.exe" } else { "git" })
.args(["describe", "--match=v*\\.*\\.*"])
.args(["describe", r"--match=v*\.*\.*"])
.output()
.ok()
.and_then(|out| parse_describe(&out.stdout))
Expand Down
2 changes: 1 addition & 1 deletion gix-actor/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub(crate) mod write {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub(crate) enum Error {
#[error("Signature name or email must not contain '<', '>' or \\n")]
#[error(r"Signature name or email must not contain '<', '>' or \n")]
IllegalCharacter,
}

Expand Down
2 changes: 1 addition & 1 deletion gix-actor/tests/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn lenient_parsing() -> gix_testtools::Result {
let err = signature.write_to(&mut output).unwrap_err();
assert_eq!(
err.to_string(),
"Signature name or email must not contain '<', '>' or \\n",
r"Signature name or email must not contain '<', '>' or \n",
"this isn't roundtrippable as the name is technically incorrect - must not contain brackets"
);
}
Expand Down
2 changes: 1 addition & 1 deletion gix-attributes/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod error {
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
#[error("Line {line_number} has a negative pattern, for literal characters use \\!: {line}")]
#[error(r"Line {line_number} has a negative pattern, for literal characters use \!: {line}")]
PatternNegation { line_number: usize, line: BString },
#[error("Attribute in line {line_number} has non-ascii characters or starts with '-': {attribute}")]
AttributeName { line_number: usize, attribute: BString },
Expand Down
2 changes: 1 addition & 1 deletion gix-attributes/tests/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn exclamation_marks_must_be_escaped_or_error_unlike_gitignore() {

#[test]
fn invalid_escapes_in_quotes_are_an_error() {
assert!(matches!(try_line(r#""\!hello""#), Err(parse::Error::Unquote(_)),),);
assert!(matches!(try_line(r#""\!hello""#), Err(parse::Error::Unquote(_))));
assert!(lenient_lines(r#""\!hello""#).is_empty());
}

Expand Down
6 changes: 3 additions & 3 deletions gix-command/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ mod prepare {
prep.command = gix_path::from_bstring(gix_quote::single(command)).into();
}
}
prep.command.push(" \"$@\"");
prep.command.push(r#" "$@""#);
} else {
gix_trace::debug!(
"Will not add '\"$@\"' to '{:?}' as it seems to contain '$@' already",
r#"Will not add '"$@"' to '{:?}' as it seems to contain '$@' already"#,
prep.command
);
}
Expand Down Expand Up @@ -434,7 +434,7 @@ pub mod shebang {
let mut line = buf.lines().next()?;
line = line.strip_prefix(b"#!")?;

let slash_idx = line.rfind_byteset(b"/\\")?;
let slash_idx = line.rfind_byteset(br"/\")?;
Some(match line[slash_idx..].find_byte(b' ') {
Some(space_idx) => {
let space = slash_idx + space_idx;
Expand Down
34 changes: 17 additions & 17 deletions gix-command/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ mod shebang {
"trimming works for tabs as well"
);
assert_eq!(
parse("#!\\bin\\sh"),
exe("\\bin\\sh"),
parse(r"#!\bin\sh"),
exe(r"\bin\sh"),
"backslashes are recognized as path separator"
);
assert_eq!(
parse("#!C:\\Program Files\\shell.exe\r\nsome stuff"),
exe("C:\\Program Files\\shell.exe"),
exe(r"C:\Program Files\shell.exe"),
"absolute windows paths are fine"
);
assert_eq!(
Expand Down Expand Up @@ -192,8 +192,8 @@ mod context {
#[test]
fn glob_pathspecs_sets_env_only() {
for (value, expected) in [
(false, "GIT_NOGLOB_PATHSPECS=\"1\""),
(true, "GIT_GLOB_PATHSPECS=\"1\""),
(false, r#"GIT_NOGLOB_PATHSPECS="1""#),
(true, r#"GIT_GLOB_PATHSPECS="1""#),
] {
let ctx = Context {
glob_pathspecs: Some(value),
Expand Down Expand Up @@ -305,7 +305,7 @@ mod prepare {
#[test]
fn single_and_complex_arguments_as_part_of_command_with_shell() {
let cmd = std::process::Command::from(
gix_command::prepare("ls --foo \"a b\"")
gix_command::prepare(r#"ls --foo "a b""#)
.arg("additional")
.command_may_be_shell_script(),
);
Expand All @@ -324,7 +324,7 @@ mod prepare {
#[test]
fn single_and_complex_arguments_with_auto_split() {
let cmd = std::process::Command::from(
gix_command::prepare("ls --foo=\"a b\"").command_may_be_shell_script_allow_manual_argument_splitting(),
gix_command::prepare(r#"ls --foo="a b""#).command_may_be_shell_script_allow_manual_argument_splitting(),
);
assert_eq!(
format!("{cmd:?}"),
Expand All @@ -336,7 +336,7 @@ mod prepare {
#[test]
fn single_and_complex_arguments_without_auto_split() {
let cmd = std::process::Command::from(
gix_command::prepare("ls --foo=\"a b\"").command_may_be_shell_script_disallow_manual_argument_splitting(),
gix_command::prepare(r#"ls --foo="a b""#).command_may_be_shell_script_disallow_manual_argument_splitting(),
);
assert_eq!(format!("{cmd:?}"), quoted(&[*SH, "-c", r#"ls --foo=\"a b\""#, "--"]));
}
Expand All @@ -351,7 +351,7 @@ mod prepare {
);
assert_eq!(
format!("{cmd:?}"),
quoted(&[*SH, "-c", "ls \\\"$@\\\"", "--", "--foo=a b"])
quoted(&[*SH, "-c", r#"ls \"$@\""#, "--", "--foo=a b"])
);
}

Expand All @@ -366,15 +366,15 @@ mod prepare {
);
assert_eq!(
format!("{cmd:?}"),
quoted(&[*SH, "-c", "\\'ls\\' \\\"$@\\\"", "--", "--foo=a b"]),
quoted(&[*SH, "-c", r#"\'ls\' \"$@\""#, "--", "--foo=a b"]),
"looks strange thanks to debug printing, but is the right amount of quotes actually"
);
}

#[test]
fn quoted_windows_command_without_argument_splitting() {
let cmd = std::process::Command::from(
gix_command::prepare("C:\\Users\\O'Shaughnessy\\with space.exe")
gix_command::prepare(r"C:\Users\O'Shaughnessy\with space.exe")
.arg("--foo='a b'")
.command_may_be_shell_script_disallow_manual_argument_splitting()
.with_shell()
Expand All @@ -385,9 +385,9 @@ mod prepare {
quoted(&[
*SH,
"-c",
"\\'C:\\\\Users\\\\O\\'\\\\\\'\\'Shaughnessy\\\\with space.exe\\' \\\"$@\\\"",
r#"\'C:\\Users\\O\'\\\'\'Shaughnessy\\with space.exe\' \"$@\""#,
"--",
"--foo=\\'a b\\'"
r"--foo=\'a b\'"
]),
"again, a lot of extra backslashes, but it's correct outside of the debug formatting"
);
Expand All @@ -409,7 +409,7 @@ mod prepare {
#[test]
fn tilde_path_and_multiple_arguments_as_part_of_command_with_shell() {
let cmd =
std::process::Command::from(gix_command::prepare("~/bin/exe --foo \"a b\"").command_may_be_shell_script());
std::process::Command::from(gix_command::prepare(r#"~/bin/exe --foo "a b""#).command_may_be_shell_script());
let sh = *SH;
assert_eq!(
format!("{cmd:?}"),
Expand All @@ -421,7 +421,7 @@ mod prepare {
#[test]
fn script_with_dollar_at() {
let cmd = std::process::Command::from(
gix_command::prepare("echo \"$@\" >&2")
gix_command::prepare(r#"echo "$@" >&2"#)
.command_may_be_shell_script()
.arg("store"),
);
Expand All @@ -437,7 +437,7 @@ mod prepare {
#[test]
fn script_with_dollar_at_has_no_quoting() {
let cmd = std::process::Command::from(
gix_command::prepare("echo \"$@\" >&2")
gix_command::prepare(r#"echo "$@" >&2"#)
.command_may_be_shell_script()
.with_quoted_command()
.arg("store"),
Expand Down Expand Up @@ -487,7 +487,7 @@ mod spawn {
#[test]
fn script_with_dollar_at() -> crate::Result {
let out = std::process::Command::from(
gix_command::prepare("echo \"$@\"")
gix_command::prepare(r#"echo "$@""#)
.command_may_be_shell_script()
.arg("arg"),
)
Expand Down
8 changes: 4 additions & 4 deletions gix-config-value/tests/value/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod interpolate {

#[test]
fn backslash_is_not_special_and_they_are_not_escaping_anything() -> crate::Result {
for path in ["C:\\foo\\bar", "/foo/bar"] {
for path in [r"C:\foo\bar", "/foo/bar"] {
let actual = gix_config_value::Path::from(Cow::Borrowed(b(path))).interpolate(Default::default())?;
assert_eq!(actual, Path::new(path));
assert!(
Expand All @@ -31,8 +31,8 @@ mod interpolate {

#[test]
fn prefix_substitutes_git_install_dir() {
for git_install_dir in &["/tmp/git", "C:\\git"] {
for (val, expected) in &[("%(prefix)/foo/bar", "foo/bar"), ("%(prefix)/foo\\bar", "foo\\bar")] {
for git_install_dir in &["/tmp/git", r"C:\git"] {
for (val, expected) in &[("%(prefix)/foo/bar", "foo/bar"), (r"%(prefix)/foo\bar", r"foo\bar")] {
let expected =
std::path::PathBuf::from(format!("{}{}{}", git_install_dir, std::path::MAIN_SEPARATOR, expected));
assert_eq!(
Expand Down Expand Up @@ -103,7 +103,7 @@ mod interpolate {
fn tilde_with_given_user() -> crate::Result {
let home = std::env::current_dir()?;

for path_suffix in &["foo/bar", "foo\\bar", ""] {
for path_suffix in &["foo/bar", r"foo\bar", ""] {
let path = format!("~user{}{}", std::path::MAIN_SEPARATOR, path_suffix);
let expected = home.join("user").join(path_suffix);

Expand Down
8 changes: 4 additions & 4 deletions gix-config/src/file/mutable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ fn escape_value(value: &BStr) -> BString {

for b in value.iter().copied() {
match b {
b'\n' => buf.push_str("\\n"),
b'\t' => buf.push_str("\\t"),
b'"' => buf.push_str("\\\""),
b'\\' => buf.push_str("\\\\"),
b'\n' => buf.push_str(r"\n"),
b'\t' => buf.push_str(r"\t"),
b'"' => buf.push_str(r#"\""#),
b'\\' => buf.push_str(r"\\"),
_ => buf.push(b),
}
}
Expand Down
2 changes: 1 addition & 1 deletion gix-config/src/parse/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Event<'_> {
match self {
Self::ValueNotDone(e) => {
out.write_all(e.as_ref())?;
out.write_all(b"\\")
out.write_all(br"\")
}
Self::Whitespace(e) | Self::Newline(e) | Self::Value(e) | Self::ValueDone(e) => out.write_all(e.as_ref()),
Self::KeyValueSeparator => out.write_all(b"="),
Expand Down
20 changes: 10 additions & 10 deletions gix-config/src/parse/nom/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mod section_headers {

#[test]
fn eof_after_escape_in_sub_section() {
assert!(section_header.parse_peek(b"[hello \"hello\\").is_err());
assert!(section_header.parse_peek(br#"[hello "hello\"#).is_err());
}

#[test]
Expand Down Expand Up @@ -124,7 +124,7 @@ mod sub_section {

#[test]
fn zero_copy_simple() {
let actual = sub_section.parse_peek(b"name\"").unwrap().1;
let actual = sub_section.parse_peek(br#"name""#).unwrap().1;
assert_eq!(actual.as_ref(), "name");
assert!(matches!(actual, Cow::Borrowed(_)));
}
Expand Down Expand Up @@ -301,7 +301,7 @@ mod section {
whitespace_event(" "),
Event::KeyValueSeparator,
whitespace_event(" "),
value_event("\"lol\"")
value_event(r#""lol""#)
]
})
);
Expand Down Expand Up @@ -370,7 +370,7 @@ mod section {
whitespace_event(" "),
Event::KeyValueSeparator,
whitespace_event(" "),
value_event("\"lol\"")
value_event(r#""lol""#)
]
})
);
Expand Down Expand Up @@ -567,7 +567,7 @@ mod value_continuation {
let mut events = Vec::new();
assert!(
value_impl(b"hello\\\r\r\n world", &mut events).is_err(),
"\\r must be followed by \\n"
r"\r must be followed by \n"
);
}

Expand Down Expand Up @@ -634,9 +634,9 @@ mod value_continuation {
vec![
value_not_done_event("1"),
newline_event(),
value_not_done_event("\"2\" a"),
value_not_done_event(r#""2" a"#),
newline_event(),
value_not_done_event("\\\"3 b\\\""),
value_not_done_event(r#"\"3 b\""#),
newline_event(),
value_done_event("4")
]
Expand Down Expand Up @@ -664,9 +664,9 @@ mod value_continuation {
vec![
value_not_done_event("\"1"),
newline_event(),
value_not_done_event("\"2\" a"),
value_not_done_event(r#""2" a"#),
newline_event(),
value_not_done_event("\\\"3 b\\\""),
value_not_done_event(r#"\"3 b\""#),
newline_event(),
value_done_event("4 \"")
]
Expand Down Expand Up @@ -761,7 +761,7 @@ mod value_no_continuation {

#[test]
fn garbage_after_continuation_is_err() {
assert!(value_impl(b"hello \\afwjdls", &mut Default::default()).is_err());
assert!(value_impl(br"hello \afwjdls", &mut Default::default()).is_err());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions gix-config/src/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ mod section {

#[test]
fn legacy_subsection_format_does_not_use_escapes() {
let invalid = header("invalid", Some((".", "\\ \"")));
let invalid = header("invalid", Some((".", r#"\ ""#)));
assert_eq!(
invalid.to_bstring(),
"[invalid.\\ \"]",
r#"[invalid.\ "]"#,
"no escaping happens for legacy subsections"
);
assert!(invalid.is_legacy());
Expand Down
2 changes: 1 addition & 1 deletion gix-config/tests/config/file/access/read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn get_value_for_all_provided_values() -> crate::Result {
&[cow_str("")],
"unset values show up as empty within a string array"
);
assert_eq!(config.strings("core.bool-implicit").expect("present"), &[cow_str("")],);
assert_eq!(config.strings("core.bool-implicit").expect("present"), &[cow_str("")]);

assert_eq!(config.string("doesn't.exist"), None);

Expand Down
2 changes: 1 addition & 1 deletion gix-config/tests/config/file/init/comfort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn from_git_dir() -> crate::Result {
"value",
"a value from the local repo configuration"
);
assert_eq!(config.string("a.local").expect("present").as_ref(), "value",);
assert_eq!(config.string("a.local").expect("present").as_ref(), "value");
assert_eq!(
config.string_by("a", None, "local-include").expect("present").as_ref(),
"from-a.config",
Expand Down
8 changes: 4 additions & 4 deletions gix-config/tests/config/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fn valid_and_invalid() {
assert_eq!(key.subsection_name.unwrap(), "quux.bar");
assert_eq!(key.value_name, "baz");

let key = "includeIf.gitdir/i:C:\\bare.git.path".as_key();
assert_eq!(key.subsection_name, Some("gitdir/i:C:\\bare.git".into()),);
let key = r"includeIf.gitdir/i:C:\bare.git.path".as_key();
assert_eq!(key.subsection_name, Some(r"gitdir/i:C:\bare.git".into()));
}

mod _ref {
Expand Down Expand Up @@ -56,10 +56,10 @@ mod _ref {
);

assert_eq!(
KeyRef::parse_unvalidated("includeIf.gitdir/i:C:\\bare.git.path".into()),
KeyRef::parse_unvalidated(r"includeIf.gitdir/i:C:\bare.git.path".into()),
Some(KeyRef {
section_name: "includeIf",
subsection_name: Some("gitdir/i:C:\\bare.git".into()),
subsection_name: Some(r"gitdir/i:C:\bare.git".into()),
value_name: "path"
})
);
Expand Down
2 changes: 1 addition & 1 deletion gix-config/tests/config/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn git_config_no_system() {
_ => unreachable!("known set"),
}
})
.is_some(),);
.is_some());
}

#[test]
Expand Down
Loading
Loading