@@ -1497,8 +1497,7 @@ impl Url {
1497
1497
/// # run().unwrap();
1498
1498
/// ```
1499
1499
pub fn set_port ( & mut self , mut port : Option < u16 > ) -> Result < ( ) , ( ) > {
1500
- // has_host implies !cannot_be_a_base
1501
- if !self . has_host ( ) || self . host ( ) == Some ( Host :: Domain ( "" ) ) || self . scheme ( ) == "file" {
1500
+ if self . cannot_have_username_password_port ( ) {
1502
1501
return Err ( ( ) ) ;
1503
1502
}
1504
1503
if port. is_some ( ) && port == parser:: default_port ( self . scheme ( ) ) {
@@ -1807,8 +1806,7 @@ impl Url {
1807
1806
/// # run().unwrap();
1808
1807
/// ```
1809
1808
pub fn set_password ( & mut self , password : Option < & str > ) -> Result < ( ) , ( ) > {
1810
- // has_host implies !cannot_be_a_base
1811
- if !self . has_host ( ) || self . host ( ) == Some ( Host :: Domain ( "" ) ) || self . scheme ( ) == "file" {
1809
+ if self . cannot_have_username_password_port ( ) {
1812
1810
return Err ( ( ) ) ;
1813
1811
}
1814
1812
if let Some ( password) = password {
@@ -1899,8 +1897,7 @@ impl Url {
1899
1897
/// # run().unwrap();
1900
1898
/// ```
1901
1899
pub fn set_username ( & mut self , username : & str ) -> Result < ( ) , ( ) > {
1902
- // has_host implies !cannot_be_a_base
1903
- if !self . has_host ( ) || self . host ( ) == Some ( Host :: Domain ( "" ) ) || self . scheme ( ) == "file" {
1900
+ if self . cannot_have_username_password_port ( ) {
1904
1901
return Err ( ( ) ) ;
1905
1902
}
1906
1903
let username_start = self . scheme_end + 3 ;
@@ -2361,6 +2358,12 @@ impl Url {
2361
2358
2362
2359
// Private helper methods:
2363
2360
2361
+ fn cannot_have_username_password_port ( & self ) -> bool {
2362
+ self . host ( ) . unwrap_or ( Host :: Domain ( "" ) ) == Host :: Domain ( "" )
2363
+ || self . cannot_be_a_base ( )
2364
+ || self . scheme ( ) == "file"
2365
+ }
2366
+
2364
2367
#[ inline]
2365
2368
fn slice < R > ( & self , range : R ) -> & str
2366
2369
where
0 commit comments