@@ -1428,8 +1428,7 @@ impl Url {
1428
1428
/// # run().unwrap();
1429
1429
/// ```
1430
1430
pub fn set_port ( & mut self , mut port : Option < u16 > ) -> Result < ( ) , ( ) > {
1431
- // has_host implies !cannot_be_a_base
1432
- if !self . has_host ( ) || self . host ( ) == Some ( Host :: Domain ( "" ) ) || self . scheme ( ) == "file" {
1431
+ if self . cannot_have_username_password_port ( ) {
1433
1432
return Err ( ( ) ) ;
1434
1433
}
1435
1434
if port. is_some ( ) && port == parser:: default_port ( self . scheme ( ) ) {
@@ -1719,8 +1718,7 @@ impl Url {
1719
1718
/// # run().unwrap();
1720
1719
/// ```
1721
1720
pub fn set_password ( & mut self , password : Option < & str > ) -> Result < ( ) , ( ) > {
1722
- // has_host implies !cannot_be_a_base
1723
- if !self . has_host ( ) || self . host ( ) == Some ( Host :: Domain ( "" ) ) || self . scheme ( ) == "file" {
1721
+ if self . cannot_have_username_password_port ( ) {
1724
1722
return Err ( ( ) ) ;
1725
1723
}
1726
1724
if let Some ( password) = password {
@@ -1811,8 +1809,7 @@ impl Url {
1811
1809
/// # run().unwrap();
1812
1810
/// ```
1813
1811
pub fn set_username ( & mut self , username : & str ) -> Result < ( ) , ( ) > {
1814
- // has_host implies !cannot_be_a_base
1815
- if !self . has_host ( ) || self . host ( ) == Some ( Host :: Domain ( "" ) ) || self . scheme ( ) == "file" {
1812
+ if self . cannot_have_username_password_port ( ) {
1816
1813
return Err ( ( ) ) ;
1817
1814
}
1818
1815
let username_start = self . scheme_end + 3 ;
@@ -2210,6 +2207,12 @@ impl Url {
2210
2207
2211
2208
// Private helper methods:
2212
2209
2210
+ fn cannot_have_username_password_port ( & self ) -> bool {
2211
+ self . host ( ) . unwrap_or ( Host :: Domain ( "" ) ) == Host :: Domain ( "" )
2212
+ || self . cannot_be_a_base ( )
2213
+ || self . scheme ( ) == "file"
2214
+ }
2215
+
2213
2216
#[ inline]
2214
2217
fn slice < R > ( & self , range : R ) -> & str
2215
2218
where
0 commit comments