@@ -1497,8 +1497,7 @@ impl Url {
14971497 /// # run().unwrap();
14981498 /// ```
14991499 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 ( ) {
15021501 return Err ( ( ) ) ;
15031502 }
15041503 if port. is_some ( ) && port == parser:: default_port ( self . scheme ( ) ) {
@@ -1807,8 +1806,7 @@ impl Url {
18071806 /// # run().unwrap();
18081807 /// ```
18091808 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 ( ) {
18121810 return Err ( ( ) ) ;
18131811 }
18141812 if let Some ( password) = password {
@@ -1899,8 +1897,7 @@ impl Url {
18991897 /// # run().unwrap();
19001898 /// ```
19011899 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 ( ) {
19041901 return Err ( ( ) ) ;
19051902 }
19061903 let username_start = self . scheme_end + 3 ;
@@ -2361,6 +2358,12 @@ impl Url {
23612358
23622359 // Private helper methods:
23632360
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+
23642367 #[ inline]
23652368 fn slice < R > ( & self , range : R ) -> & str
23662369 where
0 commit comments