@@ -1428,8 +1428,7 @@ impl Url {
14281428 /// # run().unwrap();
14291429 /// ```
14301430 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 ( ) {
14331432 return Err ( ( ) ) ;
14341433 }
14351434 if port. is_some ( ) && port == parser:: default_port ( self . scheme ( ) ) {
@@ -1719,8 +1718,7 @@ impl Url {
17191718 /// # run().unwrap();
17201719 /// ```
17211720 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 ( ) {
17241722 return Err ( ( ) ) ;
17251723 }
17261724 if let Some ( password) = password {
@@ -1811,8 +1809,7 @@ impl Url {
18111809 /// # run().unwrap();
18121810 /// ```
18131811 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 ( ) {
18161813 return Err ( ( ) ) ;
18171814 }
18181815 let username_start = self . scheme_end + 3 ;
@@ -2210,6 +2207,12 @@ impl Url {
22102207
22112208 // Private helper methods:
22122209
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+
22132216 #[ inline]
22142217 fn slice < R > ( & self , range : R ) -> & str
22152218 where
0 commit comments