Git diff doesn't work well with Rust

Did ever someone notice that git diff doesn't work very well with Rust's syntax?

Changing

fn foo<T>(x: T)
where
    T: Clone,
{
    alpha();
    beta();
    charlie();
}

to

fn foo<T>(x: T)
where
    T: Clone,
{
    alpha();
    beta();
    charlie();
    delta();
}

and running git diff, gives:

diff --git a/src/main.rs b/src/main.rs
index 095981f..7434568 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,4 +5,5 @@ where
     alpha();
     beta();
     charlie();
+    delta();
 }

Note the @@ where, which is pretty useless. :frowning_face:

When not using the where syntax, however, git diff works fine:

diff --git a/src/main.rs b/src/main.rs
index 0ed369a..7f50f46 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,4 +2,5 @@ fn foo<T: Clone>(x: T) {
     alpha();
     beta();
     charlie();
+    delta();
 }

Here, the diff output correctly shows the function we're modifying (@@ fn foo…).

4 Likes

It looks like userdiff is a bit overly lenient with what it considers a section header. Any idea what sort of pattern this second line is intended to match?

4 Likes

Do you have *.rs diff=rust in .gitattributes?

7 Likes

That helps indeed! I solved it now for all repositories using:

mkdir -p ~/.config/git
echo '*.rs diff=rust' >> ~/.config/git/attributes

(on Linux/BSD)

2 Likes

Should cargo new create such a .gitattributes file? I notice that the Rust source contains one too.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.