Repository: rust-lang/rust
File: src/librustdoc/clean/types.rs (Rust)

Code before:
                    Some(p) => p.to_path_buf(),
                    None => PathBuf::new(),
                }
            }
            _ => PathBuf::new(),
        }
    }

    /// Attempts to find where an external crate is located, given that we're
    /// rendering into the specified source destination.
    pub(crate) fn location(
        &self,
        extern_url: Option<&str>,
        extern_url_takes_precedence: bool,
        dst: &std::path::Path,
        tcx: TyCtxt<'_>,
    ) -> ExternalLocation {
        use ExternalLocation::*;

        fn to_remote(url: impl ToString) -> ExternalLocation {
            let mut url = url.to_string();
            if !url.ends_with('/') {
                url.push('/');
            }
            let is_absolute = url.starts_with('/')
                || url.split_once("://").is_some_and(|(scheme, _)| {
                    scheme.bytes().next().is_some_and(|b| b.is_ascii_alphabetic())
                        && scheme
                            .bytes()
                            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
                });
            Remote { url, is_absolute }
        }

        // See if there's documentation generated into the local directory
        // WARNING: since rustdoc creates these directories as it generates documentation, this check is only accurate before rendering starts.
        // Make sure to call `location()` by that time.
        let local_location = dst.join(self.name(tcx).as_str());
        if local_location.is_dir() {
            return Local;
        }

        if extern_url_takes_precedence && let Some(url) = extern_url {
            return to_remote(url);
        }

        // Failing that, see if there's an attribute specifying where to find this
        // external crate
        let did = self.crate_num.as_def_id();
        find_attr!(tcx, did, Doc(d) =>d.html_root_url.map(|(url, _)| url))
            .flatten()

Review comment (suggestion):
```suggestion
                || url.split_once(':').is_some_and(|(scheme, _)| {
```

The IETF URL standard might ascribe special meaning to `//`, but the URL standard we care about is the one web browsers use, which lets you write `<a href="http:rust-lang.org">this</a>` and you get <a href="http:rust-lang.org">this</a>.

Code after:
                    Some(p) => p.to_path_buf(),
                    None => PathBuf::new(),
                }
            }
            _ => PathBuf::new(),
        }
    }

    /// Attempts to find where an external crate is located, given that we're
    /// rendering into the specified source destination.
    pub(crate) fn location(
        &self,
        extern_url: Option<&str>,
        extern_url_takes_precedence: bool,
        dst: &std::path::Path,
        tcx: TyCtxt<'_>,
    ) -> ExternalLocation {
        use ExternalLocation::*;

        fn to_remote(url: impl ToString) -> ExternalLocation {
            let mut url = url.to_string();
            if !url.ends_with('/') {
                url.push('/');
            }
            let is_absolute = url.starts_with('/')
                || url.split_once(':').is_some_and(|(scheme, _)| {
                    scheme.bytes().next().is_some_and(|b| b.is_ascii_alphabetic())
                        && scheme
                            .bytes()
                            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
                });
            Remote { url, is_absolute }
        }

        // See if there's documentation generated into the local directory
        // WARNING: since rustdoc creates these directories as it generates documentation, this check is only accurate before rendering starts.
        // Make sure to call `location()` by that time.
        let local_location = dst.join(self.name(tcx).as_str());
        if local_location.is_dir() {
            return Local;
        }

        if extern_url_takes_precedence && let Some(url) = extern_url {
            return to_remote(url);
        }

        // Failing that, see if there's an attribute specifying where to find this
        // external crate
        let did = self.crate_num.as_def_id();
        find_attr!(tcx, did, Doc(d) =>d.html_root_url.map(|(url, _)| url))
            .flatten()